#include <cstdio> #include <algorithm> #include <vector> #include <list> #include <functional> using namespace std; struct Backpack { int size; int free; list<int> items; bool operator>(const Backpack& other) const { return size > other.size; } }; void getInput(list<int>& items, vector<Backpack>& backpacks) { int n, m; scanf("%d %d", &n, &m); while (n--) { int a; scanf("%d", &a); items.push_back(a); } while (m--) { Backpack c; scanf("%d", &c.size); c.free = c.size; backpacks.push_back(c); } items.sort(greater<int>()); sort(backpacks.begin(), backpacks.end(), greater<Backpack>()); while(backpacks.size() > items.size()) { backpacks.pop_back(); } } bool isEnoughSpaceInBackpacks(const list<int>& items, const vector<Backpack>& backpacks) { if (*items.begin() > backpacks.begin()->size) { return false; } long long int itemsSize = 0; long long int backpacksSize = 0; for (list<int>::const_iterator it = items.begin(); it != items.end(); ++it) { itemsSize += *it; } for (vector<Backpack>::const_iterator it = backpacks.begin(); it != backpacks.end(); ++it) { backpacksSize += it->size; } return (itemsSize <= backpacksSize); } int processFast(list<int>& items, vector<Backpack>& backpacks, int& perfectResult) { long long int itemsSize = 0; long long int backpacksSize = backpacks[0].size; int start = 0; for (list<int>::const_iterator it = items.begin(); it != items.end(); ++it) { itemsSize += *it; } while (itemsSize > backpacksSize) { backpacksSize += backpacks[++start].size; } perfectResult = start+1; while (start < backpacks.size()) { for (int i = start; i >= 0; --i) { list<int>::iterator it = items.begin(); list<int>::iterator thiz; while (it != items.end()) { thiz = it; ++it; if (*thiz <= backpacks[i].free) { backpacks[i].items.push_back(*thiz); backpacks[i].free -= *thiz; items.erase(thiz); } } } if (items.size() == 0) { return start+1; } else if (*items.begin() > backpacks[start].size) { return -1; } for (int i = start; i >= 0; --i) { items.merge(backpacks[i].items, greater<int>()); backpacks[i].free = backpacks[i].size; } ++start; } return -1; } int mergeBacksacks(vector<Backpack>& backpacks, const int perfectResult, const int currentResult) { for (int i = 0; i < currentResult; ++i) { int k = 0; while (k >= 0) { k = -1; for (int j = i+1; j < currentResult; ++j) { if ((backpacks[j].size != backpacks[j].free) && (backpacks[i].free >= (backpacks[j].size - backpacks[j].free))) { if (k == -1 || ((backpacks[j].size - backpacks[j].free) > (backpacks[k].size - backpacks[k].free))) { k = j; } } } if (k != -1) { backpacks[i].items.merge(backpacks[k].items, greater<int>()); backpacks[i].free -= (backpacks[k].size - backpacks[k].free); backpacks[k].free = backpacks[k].size; } } } int fixedResult = currentResult; for (int i = 0; i < currentResult; ++i) { if (backpacks[i].size == backpacks[i].free) { fixedResult--; } } return fixedResult; } int process(list<int>& items, vector<Backpack>& backpacks) { int perfectResult; int result = processFast(items, backpacks, perfectResult); if (result > 0 && result > perfectResult) { result = mergeBacksacks(backpacks, perfectResult, result); } return result; } int main() { list<int> items; vector<Backpack> backpacks; getInput(items, backpacks); if (!isEnoughSpaceInBackpacks(items, backpacks)) { printf("NIE\n"); return 0; } int result = process(items, backpacks); if (result > 0) { printf("%d\n", result); } 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 | #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <functional> using namespace std; struct Backpack { int size; int free; list<int> items; bool operator>(const Backpack& other) const { return size > other.size; } }; void getInput(list<int>& items, vector<Backpack>& backpacks) { int n, m; scanf("%d %d", &n, &m); while (n--) { int a; scanf("%d", &a); items.push_back(a); } while (m--) { Backpack c; scanf("%d", &c.size); c.free = c.size; backpacks.push_back(c); } items.sort(greater<int>()); sort(backpacks.begin(), backpacks.end(), greater<Backpack>()); while(backpacks.size() > items.size()) { backpacks.pop_back(); } } bool isEnoughSpaceInBackpacks(const list<int>& items, const vector<Backpack>& backpacks) { if (*items.begin() > backpacks.begin()->size) { return false; } long long int itemsSize = 0; long long int backpacksSize = 0; for (list<int>::const_iterator it = items.begin(); it != items.end(); ++it) { itemsSize += *it; } for (vector<Backpack>::const_iterator it = backpacks.begin(); it != backpacks.end(); ++it) { backpacksSize += it->size; } return (itemsSize <= backpacksSize); } int processFast(list<int>& items, vector<Backpack>& backpacks, int& perfectResult) { long long int itemsSize = 0; long long int backpacksSize = backpacks[0].size; int start = 0; for (list<int>::const_iterator it = items.begin(); it != items.end(); ++it) { itemsSize += *it; } while (itemsSize > backpacksSize) { backpacksSize += backpacks[++start].size; } perfectResult = start+1; while (start < backpacks.size()) { for (int i = start; i >= 0; --i) { list<int>::iterator it = items.begin(); list<int>::iterator thiz; while (it != items.end()) { thiz = it; ++it; if (*thiz <= backpacks[i].free) { backpacks[i].items.push_back(*thiz); backpacks[i].free -= *thiz; items.erase(thiz); } } } if (items.size() == 0) { return start+1; } else if (*items.begin() > backpacks[start].size) { return -1; } for (int i = start; i >= 0; --i) { items.merge(backpacks[i].items, greater<int>()); backpacks[i].free = backpacks[i].size; } ++start; } return -1; } int mergeBacksacks(vector<Backpack>& backpacks, const int perfectResult, const int currentResult) { for (int i = 0; i < currentResult; ++i) { int k = 0; while (k >= 0) { k = -1; for (int j = i+1; j < currentResult; ++j) { if ((backpacks[j].size != backpacks[j].free) && (backpacks[i].free >= (backpacks[j].size - backpacks[j].free))) { if (k == -1 || ((backpacks[j].size - backpacks[j].free) > (backpacks[k].size - backpacks[k].free))) { k = j; } } } if (k != -1) { backpacks[i].items.merge(backpacks[k].items, greater<int>()); backpacks[i].free -= (backpacks[k].size - backpacks[k].free); backpacks[k].free = backpacks[k].size; } } } int fixedResult = currentResult; for (int i = 0; i < currentResult; ++i) { if (backpacks[i].size == backpacks[i].free) { fixedResult--; } } return fixedResult; } int process(list<int>& items, vector<Backpack>& backpacks) { int perfectResult; int result = processFast(items, backpacks, perfectResult); if (result > 0 && result > perfectResult) { result = mergeBacksacks(backpacks, perfectResult, result); } return result; } int main() { list<int> items; vector<Backpack> backpacks; getInput(items, backpacks); if (!isEnoughSpaceInBackpacks(items, backpacks)) { printf("NIE\n"); return 0; } int result = process(items, backpacks); if (result > 0) { printf("%d\n", result); } else { printf("NIE\n"); } return 0; } |