#include <cstdio> #include <algorithm> #include <functional> using namespace std; int n, m; int t[30]; int c[110]; int ini[30]; int dfs(int p[], int k, int l) { if (k == n) { return 1; } int p2[l]; for (int i = 0; i < l; i++) { if (p[i] + t[k] <= c[i]) { for (int j = 0; j < l; j++) { p2[j] = p[j]; } p2[i] += t[k]; if (dfs(p2, k+1, l)) return 1; } } return 0; } int main() { scanf("%d%d",&n,&m); for (int i = 0; i < n; i++) { scanf("%d",&t[i]); } for (int i = 0; i < m; i++) { scanf("%d",&c[i]); } sort(t, t+n, greater<int>()); sort(c, c+m, greater<int>()); m = std::min(n, m); int a = 1, b = m+1; while (a < b) { int c = (a + b)/2; if (dfs(ini, 0, c)) b = c; else a = c+1; } if (b <= m) printf("%d",b); else printf("NIE\n"); }
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 | #include <cstdio> #include <algorithm> #include <functional> using namespace std; int n, m; int t[30]; int c[110]; int ini[30]; int dfs(int p[], int k, int l) { if (k == n) { return 1; } int p2[l]; for (int i = 0; i < l; i++) { if (p[i] + t[k] <= c[i]) { for (int j = 0; j < l; j++) { p2[j] = p[j]; } p2[i] += t[k]; if (dfs(p2, k+1, l)) return 1; } } return 0; } int main() { scanf("%d%d",&n,&m); for (int i = 0; i < n; i++) { scanf("%d",&t[i]); } for (int i = 0; i < m; i++) { scanf("%d",&c[i]); } sort(t, t+n, greater<int>()); sort(c, c+m, greater<int>()); m = std::min(n, m); int a = 1, b = m+1; while (a < b) { int c = (a + b)/2; if (dfs(ini, 0, c)) b = c; else a = c+1; } if (b <= m) printf("%d",b); else printf("NIE\n"); } |