#include<cstdio> #include<iostream> #include<vector> #include<climits> #include<algorithm> #include<map> using namespace std; map<int, int> results; bool sortFunc(int i, int j) { return i > j; } /*int hashsum(vector<int> &items, vector<int> &backpacks, vector<bool> &used) { for (items.) }*/ int minbackpacks(vector<int> &items, vector<int> &backpacks, vector<bool> &used) { if (items.size() == 0) return 0; if (backpacks.size() == 0) return INT_MAX; int minimum = INT_MAX; int tmpmin; for (int i = 0; i < items.size(); i++) { for (int j = 0; j < backpacks.size(); j++) { if (backpacks[j] >= items[i]) { tmpmin = 0; vector<int> bp = vector<int>(backpacks); vector<bool> us = vector<bool>(used); vector<int> it = vector<int>(items); if (!us[j]) { tmpmin++; us[j] = true; } bp[j] -= it[i]; if (bp[j] == 0) { bp.erase(bp.begin() + j); us.erase(us.begin() + j); } it.erase(it.begin() + i); tmpmin += minbackpacks(it, bp, us); minimum = min(minimum, tmpmin); } } } return minimum; } int main() { int n, m; int weight; scanf("%d %d", &n, &m); vector<int> items; vector<int> backpacks; vector<bool> used; for (int i = 0; i < n; i++) { scanf("%d", &weight); items.push_back(weight); } for (int i = 0; i < m; i++) { scanf("%d", &weight); backpacks.push_back(weight); used.push_back(false); } sort(backpacks.begin(), backpacks.end(), sortFunc); if (n < m) { backpacks.resize(n); used.resize(n); } int min = minbackpacks(items, backpacks, used); printf("%d\n", min); }
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 | #include<cstdio> #include<iostream> #include<vector> #include<climits> #include<algorithm> #include<map> using namespace std; map<int, int> results; bool sortFunc(int i, int j) { return i > j; } /*int hashsum(vector<int> &items, vector<int> &backpacks, vector<bool> &used) { for (items.) }*/ int minbackpacks(vector<int> &items, vector<int> &backpacks, vector<bool> &used) { if (items.size() == 0) return 0; if (backpacks.size() == 0) return INT_MAX; int minimum = INT_MAX; int tmpmin; for (int i = 0; i < items.size(); i++) { for (int j = 0; j < backpacks.size(); j++) { if (backpacks[j] >= items[i]) { tmpmin = 0; vector<int> bp = vector<int>(backpacks); vector<bool> us = vector<bool>(used); vector<int> it = vector<int>(items); if (!us[j]) { tmpmin++; us[j] = true; } bp[j] -= it[i]; if (bp[j] == 0) { bp.erase(bp.begin() + j); us.erase(us.begin() + j); } it.erase(it.begin() + i); tmpmin += minbackpacks(it, bp, us); minimum = min(minimum, tmpmin); } } } return minimum; } int main() { int n, m; int weight; scanf("%d %d", &n, &m); vector<int> items; vector<int> backpacks; vector<bool> used; for (int i = 0; i < n; i++) { scanf("%d", &weight); items.push_back(weight); } for (int i = 0; i < m; i++) { scanf("%d", &weight); backpacks.push_back(weight); used.push_back(false); } sort(backpacks.begin(), backpacks.end(), sortFunc); if (n < m) { backpacks.resize(n); used.resize(n); } int min = minbackpacks(items, backpacks, used); printf("%d\n", min); } |