#include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define st first #define nd second using namespace std; typedef unsigned int uint; typedef pair<int, uint> PIU; const int MAXN = 24; const int MAXM = 103; int n, m; uint weight[MAXN]; uint cap[MAXM]; PIU res[(1<<MAXN)+3]; //char lookup[(1<<MAXN)+3]; int main(){ scanf("%d %d", &n, &m); FWD(i,0,n){ scanf("%d", &weight[i]); // lookup[1<<i] = i; } FWD(i,1,m+1) scanf("%d", &cap[i]); sort(cap+1, cap+m+1); reverse(cap+1, cap+m+1); cap[n+1] = 0; FWD(mask,1,1<<n){ PIU b = PIU(n+1, 0); int cmask = mask; while(cmask){ // int i = lookup[cmask&(-cmask)]; int i = __builtin_ctz(cmask); cmask &= cmask-1; PIU r = res[mask&(~(1<<i))]; if(r.st >= b.st) continue; if(r.nd + weight[i] <= cap[r.st]) r.nd += weight[i]; else{ ++r.st; if(weight[i] <= cap[r.st]) r.nd = weight[i]; else continue; } b = min(b, r); } res[mask] = b; } if(res[(1<<n)-1].st == n+1) printf("NIE\n"); else printf("%d\n", res[(1<<n)-1].st); return 0; }
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 | #include <bits/stdc++.h> #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define st first #define nd second using namespace std; typedef unsigned int uint; typedef pair<int, uint> PIU; const int MAXN = 24; const int MAXM = 103; int n, m; uint weight[MAXN]; uint cap[MAXM]; PIU res[(1<<MAXN)+3]; //char lookup[(1<<MAXN)+3]; int main(){ scanf("%d %d", &n, &m); FWD(i,0,n){ scanf("%d", &weight[i]); // lookup[1<<i] = i; } FWD(i,1,m+1) scanf("%d", &cap[i]); sort(cap+1, cap+m+1); reverse(cap+1, cap+m+1); cap[n+1] = 0; FWD(mask,1,1<<n){ PIU b = PIU(n+1, 0); int cmask = mask; while(cmask){ // int i = lookup[cmask&(-cmask)]; int i = __builtin_ctz(cmask); cmask &= cmask-1; PIU r = res[mask&(~(1<<i))]; if(r.st >= b.st) continue; if(r.nd + weight[i] <= cap[r.st]) r.nd += weight[i]; else{ ++r.st; if(weight[i] <= cap[r.st]) r.nd = weight[i]; else continue; } b = min(b, r); } res[mask] = b; } if(res[(1<<n)-1].st == n+1) printf("NIE\n"); else printf("%d\n", res[(1<<n)-1].st); return 0; } |