#include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MX = 24; int n, m, a[MX*2], c[MX*10]; bool f[1<<(MX+2)], s[1<<(MX+2)]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &c[i]), c[i] = -c[i]; sort(c+1, c+m+1); for (int i = 1; i <= m; i++) c[i] = -c[i]; f[0] = true; int l = 1; while (!f[(1<<n)-1] && l <= n) { for (int i = 0; i < (1<<n); i++) if (!s[i]) for (int j = 0; j <= i; j++) if ((j&i) == j && f[i^j] == true) { unsigned int S = 0; for (int k = 0; k < n; k++) if (j&(1<<k)) S = S + a[k+1]; if (S <= (unsigned int) c[l]) s[i] = true; } l++; for (int i = 0; i < (1<<n); i++) f[i] = s[i], s[i] = 0; } if (l > n) printf("NIE\n"); else printf("%d\n", l-1); 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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MX = 24; int n, m, a[MX*2], c[MX*10]; bool f[1<<(MX+2)], s[1<<(MX+2)]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &c[i]), c[i] = -c[i]; sort(c+1, c+m+1); for (int i = 1; i <= m; i++) c[i] = -c[i]; f[0] = true; int l = 1; while (!f[(1<<n)-1] && l <= n) { for (int i = 0; i < (1<<n); i++) if (!s[i]) for (int j = 0; j <= i; j++) if ((j&i) == j && f[i^j] == true) { unsigned int S = 0; for (int k = 0; k < n; k++) if (j&(1<<k)) S = S + a[k+1]; if (S <= (unsigned int) c[l]) s[i] = true; } l++; for (int i = 0; i < (1<<n); i++) f[i] = s[i], s[i] = 0; } if (l > n) printf("NIE\n"); else printf("%d\n", l-1); return 0; } |