#include <algorithm> #include <cstdio> #include <list> using namespace std; #undef DEBUG #define FOR(E, I) for(typeof(I.begin()) E = I.begin(); E != I.end(); E++) const int MAX_N = 24; const int MAX_M = 100; int a[MAX_N]; int c[MAX_M]; int bppBestFitDecreasing(int n, int m) { sort(a, a + n, std::greater<int>()); sort(c, c + m, std::greater<int>()); list<int> bins; list<int> usedBins; for (int i = 0; i < m; bins.push_back(c[i]), i++); for (int i = 0; i < n; i++) { #ifdef DEBUG printf("Checking item of size %d\n", a[i]); #endif usedBins.sort(std::less<int>()); if (!usedBins.empty() && usedBins.back() >= a[i]) { #ifdef DEBUG printf("biggest bin %d\n", usedBins.back()); #endif FOR(ub, usedBins) { if (*ub >= a[i]) { *ub -= a[i]; } } } else { #ifdef DEBUG if (bins.empty()) { printf("no unitialized bins\n"); } else { printf("biggest unitialized bin %d\n", bins.front()); } #endif if (!bins.empty() && bins.front() >= a[i]) { usedBins.push_back(bins.front() - a[i]); bins.pop_front(); } else return -1; } } return usedBins.size(); } int bppFirstFitDecreasing(int n, int m) { sort(a, a + n, std::greater<int>()); sort(c, c + m, std::greater<int>()); list<int> bins; list<int> usedBins; for (int i = 0; i < m; bins.push_back(c[i]), i++); for (int i = 0; i < n; i++) { #ifdef DEBUG printf("Checking item of size %d\n", a[i]); #endif usedBins.sort(std::greater<int>()); if (!usedBins.empty() && usedBins.front() >= a[i]) { #ifdef DEBUG printf("biggest bin %d\n", usedBins.front()); #endif usedBins.front() -= a[i]; } else { #ifdef DEBUG if (bins.empty()) { printf("no unitialized bins\n"); } else { printf("biggest unitialized bin %d\n", bins.front()); } #endif if (!bins.empty() && bins.front() >= a[i]) { usedBins.push_back(bins.front() - a[i]); bins.pop_front(); } else return -1; } } return usedBins.size(); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; scanf("%d", &a[i++])); for (int i = 0; i < m; scanf("%d", &c[i++])); { int usedBins = min(bppFirstFitDecreasing(n, m), bppBestFitDecreasing(n, m)); if (usedBins > 0) { printf("%d\n", usedBins); } else { puts("NIE"); } } 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 | #include <algorithm> #include <cstdio> #include <list> using namespace std; #undef DEBUG #define FOR(E, I) for(typeof(I.begin()) E = I.begin(); E != I.end(); E++) const int MAX_N = 24; const int MAX_M = 100; int a[MAX_N]; int c[MAX_M]; int bppBestFitDecreasing(int n, int m) { sort(a, a + n, std::greater<int>()); sort(c, c + m, std::greater<int>()); list<int> bins; list<int> usedBins; for (int i = 0; i < m; bins.push_back(c[i]), i++); for (int i = 0; i < n; i++) { #ifdef DEBUG printf("Checking item of size %d\n", a[i]); #endif usedBins.sort(std::less<int>()); if (!usedBins.empty() && usedBins.back() >= a[i]) { #ifdef DEBUG printf("biggest bin %d\n", usedBins.back()); #endif FOR(ub, usedBins) { if (*ub >= a[i]) { *ub -= a[i]; } } } else { #ifdef DEBUG if (bins.empty()) { printf("no unitialized bins\n"); } else { printf("biggest unitialized bin %d\n", bins.front()); } #endif if (!bins.empty() && bins.front() >= a[i]) { usedBins.push_back(bins.front() - a[i]); bins.pop_front(); } else return -1; } } return usedBins.size(); } int bppFirstFitDecreasing(int n, int m) { sort(a, a + n, std::greater<int>()); sort(c, c + m, std::greater<int>()); list<int> bins; list<int> usedBins; for (int i = 0; i < m; bins.push_back(c[i]), i++); for (int i = 0; i < n; i++) { #ifdef DEBUG printf("Checking item of size %d\n", a[i]); #endif usedBins.sort(std::greater<int>()); if (!usedBins.empty() && usedBins.front() >= a[i]) { #ifdef DEBUG printf("biggest bin %d\n", usedBins.front()); #endif usedBins.front() -= a[i]; } else { #ifdef DEBUG if (bins.empty()) { printf("no unitialized bins\n"); } else { printf("biggest unitialized bin %d\n", bins.front()); } #endif if (!bins.empty() && bins.front() >= a[i]) { usedBins.push_back(bins.front() - a[i]); bins.pop_front(); } else return -1; } } return usedBins.size(); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; scanf("%d", &a[i++])); for (int i = 0; i < m; scanf("%d", &c[i++])); { int usedBins = min(bppFirstFitDecreasing(n, m), bppBestFitDecreasing(n, m)); if (usedBins > 0) { printf("%d\n", usedBins); } else { puts("NIE"); } } return 0; } |