-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitsetYesNoKnapsack.cpp
More file actions
57 lines (49 loc) · 1.22 KB
/
bitsetYesNoKnapsack.cpp
File metadata and controls
57 lines (49 loc) · 1.22 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
/*
builtin functions of GCC:
__builtin_popcount()
__builtin_parity() // true for odd number of set bits
__builtin_clz() // count leading zeros
__builtin_ctz() // count trailing zeros
*/
/*
given an n array of values, determine if W can be formed
*/
#include <bits/stdc++.h>
#define MAX_W (long long)1e6
using namespace std;
void usual(){
bool exists[MAX_W];
int n, W;
cin >>n >>W;
exists[0] = true;
while (n--) {
int x;
cin >>x;
// value x exists, so for every existing y, y + x exists
// WARNING: carry out update from right to left (updated region must not be checked)
// O(N*W)
for (int i = W-x; i >= x; i--)
if (exists[i]) exists[i+x] = true;
}
cout <<(exists[W] ? "YES" : "NO") <<"\n";
}
void optimal(){
int n, W;
cin >>n >>W;
bitset<MAX_W> exists;
exists[0] = true;
int i = 1;
while (n--) {
int x;
cin >>x;
// essentially, we must shift all true bits by x, and update the exists
// O(N * W / machine word size)
exists |= (exists << x);
}
for (int i=0; i<W; i++) cout <<exists[i];
cout <<(exists[W] ? "YES" : "NO") <<"\n";
}
int main(){
optimal();
return 0;
}