-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced partition.cpp
More file actions
69 lines (58 loc) · 1.4 KB
/
balanced partition.cpp
File metadata and controls
69 lines (58 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;
int getNegOnes(int a[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
int half = sum/2 + 1;
bool **rec = new bool*[n];
for (int i = 0; i <= n; ++i)
{
rec[i] = new bool[half];
}
for (int i = 0; i <= n; i++)
rec[i][0] = true;
for (int i = 1; i < n; i++)
{
for (int j = 1; j <= half; j++)
{
if (rec[i-1][j] || (j - a[i] >= 0 && rec[i-1][j - a[i]]))
rec[i][j] = true;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= half; j++)
{
cout<<rec[i][j]<< " ";
}
cout<<endl;
}
int k = sum;
for (int i = half; i >= 0; i--)
{
if (rec[n-1][i])
{
k = i;
break;
}
}
int ret = sum - k - k;
for (int i = n; i > 0; i--)
{
if (k - a[i-1] >= 0 && rec[i-1][k - a[i-1]])
{
k -= a[i-1];
cout<<a[i-1];
cout<<" ";
}
}
return ret;
}
int main()
{
int a[] = { 3, 5, 7, 11, 13, 7, 2, 12, 10, 3, 6 };
cout<<getNegOnes(a, sizeof(a)/sizeof(a[0]));
return 0;
}