#include <stdio.h> #include <algorithm> #include <vector> using namespace std; struct rucksack_t { int initialVol, leftVol; vector<int> items; }; vector<rucksack_t> rucksacks; bool compareRucksacks(const rucksack_t &lhs, const rucksack_t &rhs) { return lhs.initialVol > rhs.initialVol; } bool putItem(int available, int startOut, int weight) { for(int i = 0; i < available; ++i) { if(rucksacks[i].leftVol >= weight) { rucksacks[i].leftVol -= weight; rucksacks[i].items.push_back(weight); return true; } } if(startOut >= available) return false; for(int i = startOut; i < available && rucksacks[i].initialVol >= weight; ++i) { vector<rucksack_t> before = rucksacks; rucksacks[i].leftVol = rucksacks[i].initialVol; rucksacks[i].items.clear(); rucksacks[i].leftVol -= weight; rucksacks[i].items.push_back(weight); bool ok = true; for(vector<int>::iterator it = before[i].items.begin(); it != before[i].items.end(); ++it) { if(!putItem(available, i + 1, *it)) { ok = false; break; } } if(ok) return true; else rucksacks = before; } return false; } int main() { int n, m; scanf("%d %d", &n, &m); int items[n]; for(int i = 0; i < n; ++i) scanf("%d", &items[i]); sort(items, items + n); reverse(items, items + n); for(int i = 0; i < m; ++i) { rucksack_t r; scanf("%d", &r.initialVol); rucksacks.push_back(r); } sort(rucksacks.begin(), rucksacks.end(), compareRucksacks); int result = -1; for(int i = 0; i < m; ++i) { for(int j = 0; j <= i; ++j) { rucksacks[j].items.clear(); rucksacks[j].leftVol = rucksacks[j].initialVol; } bool ok = true; for(int j = 0; j < n; ++j) { if(!putItem(i + 1, 0, items[j])) { ok = false; break; } } if(ok) { result = i + 1; break; } } result == -1? printf("NIE\n") : printf("%d\n", result); 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 | #include <stdio.h> #include <algorithm> #include <vector> using namespace std; struct rucksack_t { int initialVol, leftVol; vector<int> items; }; vector<rucksack_t> rucksacks; bool compareRucksacks(const rucksack_t &lhs, const rucksack_t &rhs) { return lhs.initialVol > rhs.initialVol; } bool putItem(int available, int startOut, int weight) { for(int i = 0; i < available; ++i) { if(rucksacks[i].leftVol >= weight) { rucksacks[i].leftVol -= weight; rucksacks[i].items.push_back(weight); return true; } } if(startOut >= available) return false; for(int i = startOut; i < available && rucksacks[i].initialVol >= weight; ++i) { vector<rucksack_t> before = rucksacks; rucksacks[i].leftVol = rucksacks[i].initialVol; rucksacks[i].items.clear(); rucksacks[i].leftVol -= weight; rucksacks[i].items.push_back(weight); bool ok = true; for(vector<int>::iterator it = before[i].items.begin(); it != before[i].items.end(); ++it) { if(!putItem(available, i + 1, *it)) { ok = false; break; } } if(ok) return true; else rucksacks = before; } return false; } int main() { int n, m; scanf("%d %d", &n, &m); int items[n]; for(int i = 0; i < n; ++i) scanf("%d", &items[i]); sort(items, items + n); reverse(items, items + n); for(int i = 0; i < m; ++i) { rucksack_t r; scanf("%d", &r.initialVol); rucksacks.push_back(r); } sort(rucksacks.begin(), rucksacks.end(), compareRucksacks); int result = -1; for(int i = 0; i < m; ++i) { for(int j = 0; j <= i; ++j) { rucksacks[j].items.clear(); rucksacks[j].leftVol = rucksacks[j].initialVol; } bool ok = true; for(int j = 0; j < n; ++j) { if(!putItem(i + 1, 0, items[j])) { ok = false; break; } } if(ok) { result = i + 1; break; } } result == -1? printf("NIE\n") : printf("%d\n", result); return 0; } |