#include<stdio.h> #include<algorithm> #include<cstring> using namespace std; struct pack { unsigned int mask; long long int w; }; const bool cmp(const pack &x, const pack &y) { return x.w < y.w; } int n, m, b; int P[110]; int Z[30]; int Q[30]; int tryFitA() { for(int i=0;i<n;i++) { bool fit = false; int left = -1; int r = -1; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i] && P[j] - Z[j] > left) { left = P[j]-Z[j]; r = j; fit = true; } } if(!fit) return 1000; Z[r]-=Q[i]; } int s = 0; for(int i=0;i<m && i < 24;i++) if(Z[i] != P[i]) s++; return s; } int tryFitC() { int lastUsed = -1; for(int i=0;i<n;i++) { bool fit = false; int left = -1; int r = -1; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i] && P[j] - Z[j] > 0 && Z[j] > left) { left = Z[j]; r = j; fit = true; } } if(!fit) { if(lastUsed < m-1 && lastUsed < 23 && Z[lastUsed+1] >= Q[i]) { lastUsed++; r = lastUsed; } else return 1000; } Z[r]-=Q[i]; } return lastUsed+1; } int tryFitB() { for(int i=0;i<n;i++) { bool fit = false; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i]) { Z[j] -= Q[i]; fit = true; break; } } if(!fit) return 1000; } int s = 0; for(int i=0;i<m && i < 24;i++) if(Z[i] != P[i]) s++; return s; } int tryFitD(int q) { for(int i=0;i<n;i++) { // printf("%d\n", i); int v = 1 + (rand() % q); int oldV = v; int c = 0; while(v) { if(Z[c] >= Q[i]) { v--; } if(v) { c++; if(c == q) { if(v == oldV) return 1000; else { c = 0; oldV = v; } } } } Z[c] -= Q[i]; } return q; } int main() { srand(time(NULL)); scanf("%d %d", &n, &m); for(int i=0;i<n;i++) scanf("%d", &Q[i]); for(int i=0;i<m;i++) scanf("%d", &P[i]); sort(P, P+m, greater<int>()); sort(Q, Q+n, greater<int>()); if(m>24) m = 24; int mini = 1000; int mini2 = 1000; int mini3 = 1000; int cnt = n <= 10 ? 200000 : 500000; for(int r=0;r<cnt;r++) { memcpy(Z, P, m*sizeof(int)); int mm = tryFitA(); memcpy(Z, P, m*sizeof(int)); int mm2 = tryFitB(); memcpy(Z, P, m*sizeof(int)); int mm3 = tryFitC(); if(mm < mini) mini = mm; if(mm2 < mini) mini = mm2; if(mm3 < mini) mini = mm3; random_shuffle(Q, Q+n); } if(mini == 1000) mini = 25; sort(Q, Q+n); for(int r=0;r<cnt/5;r++) { memcpy(Z, P, m*sizeof(int)); int mm4 = tryFitD(mini-1); if(mm4 < mini) mini = mm4; } if(mini < 25) printf("%d\n", mini); else printf("NIE\n"); 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include<stdio.h> #include<algorithm> #include<cstring> using namespace std; struct pack { unsigned int mask; long long int w; }; const bool cmp(const pack &x, const pack &y) { return x.w < y.w; } int n, m, b; int P[110]; int Z[30]; int Q[30]; int tryFitA() { for(int i=0;i<n;i++) { bool fit = false; int left = -1; int r = -1; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i] && P[j] - Z[j] > left) { left = P[j]-Z[j]; r = j; fit = true; } } if(!fit) return 1000; Z[r]-=Q[i]; } int s = 0; for(int i=0;i<m && i < 24;i++) if(Z[i] != P[i]) s++; return s; } int tryFitC() { int lastUsed = -1; for(int i=0;i<n;i++) { bool fit = false; int left = -1; int r = -1; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i] && P[j] - Z[j] > 0 && Z[j] > left) { left = Z[j]; r = j; fit = true; } } if(!fit) { if(lastUsed < m-1 && lastUsed < 23 && Z[lastUsed+1] >= Q[i]) { lastUsed++; r = lastUsed; } else return 1000; } Z[r]-=Q[i]; } return lastUsed+1; } int tryFitB() { for(int i=0;i<n;i++) { bool fit = false; for(int j=0;j<m && j < 24;j++) { if(Z[j] >= Q[i]) { Z[j] -= Q[i]; fit = true; break; } } if(!fit) return 1000; } int s = 0; for(int i=0;i<m && i < 24;i++) if(Z[i] != P[i]) s++; return s; } int tryFitD(int q) { for(int i=0;i<n;i++) { // printf("%d\n", i); int v = 1 + (rand() % q); int oldV = v; int c = 0; while(v) { if(Z[c] >= Q[i]) { v--; } if(v) { c++; if(c == q) { if(v == oldV) return 1000; else { c = 0; oldV = v; } } } } Z[c] -= Q[i]; } return q; } int main() { srand(time(NULL)); scanf("%d %d", &n, &m); for(int i=0;i<n;i++) scanf("%d", &Q[i]); for(int i=0;i<m;i++) scanf("%d", &P[i]); sort(P, P+m, greater<int>()); sort(Q, Q+n, greater<int>()); if(m>24) m = 24; int mini = 1000; int mini2 = 1000; int mini3 = 1000; int cnt = n <= 10 ? 200000 : 500000; for(int r=0;r<cnt;r++) { memcpy(Z, P, m*sizeof(int)); int mm = tryFitA(); memcpy(Z, P, m*sizeof(int)); int mm2 = tryFitB(); memcpy(Z, P, m*sizeof(int)); int mm3 = tryFitC(); if(mm < mini) mini = mm; if(mm2 < mini) mini = mm2; if(mm3 < mini) mini = mm3; random_shuffle(Q, Q+n); } if(mini == 1000) mini = 25; sort(Q, Q+n); for(int r=0;r<cnt/5;r++) { memcpy(Z, P, m*sizeof(int)); int mm4 = tryFitD(mini-1); if(mm4 < mini) mini = mm4; } if(mini < 25) printf("%d\n", mini); else printf("NIE\n"); return 0; } |