forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
80 lines (65 loc) · 2.46 KB
/
D.cpp
File metadata and controls
80 lines (65 loc) · 2.46 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
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
#define TWO(X) (1<<(X))
#define CONTAIN(S,X) (S & TWO(X))
int n, a[111], f[TWO(23)], x[33];
int main() {
while (cin >> n) {
REP(i,n) cin >> a[i];
if (n == 1) {
cout << 1 << endl;
continue;
}
int res = n+1;
// f(S) = minimum number of variable, such that we can have set of variables S
// Note that when we have set S, we know that the last variable just created is the largest bit,
// and we must create the variable greater than it
REP(S,TWO(n)) f[S] = n+1;
f[1] = 1;
FOR(S,1,TWO(n)-2) {
int last = -1;
FORD(i,n-1,0) if (CONTAIN(S,i)) { last = i; break; }
int cur = f[S];
if (last == n-1) {
res = min(res, cur);
continue;
}
int next = last + 1;
bool ok = false;
int nx = 0;
REP(i,n) if (CONTAIN(S,i)) x[++nx] = a[i];
sort(x+1, x+nx+1);
int j = nx, i = 1;
while (i <= j) {
if (x[i] + x[j] == a[next]) {
ok = true;
break;
}
else if (x[i] + x[j] > a[next]) {
--j;
}
else ++i;
}
if (!ok) continue; // we cannot create the variable next
// we add new variable to store next
int S2 = S | TWO(next);
f[S2] = min(f[S2], max(cur, __builtin_popcount(S2)));
// we reuse existing variable to store next
REP(i,n) if (CONTAIN(S,i)) {
S2 = S - TWO(i) + TWO(next);
f[S2] = min(f[S2], max(cur, __builtin_popcount(S2)));
}
}
if (res > n) cout << -1 << endl;
else cout << res << endl;
}
return 0;
}