#include <algorithm> #include <string> #include <vector> #include <list> #include <cctype> #include <cmath> #include <iostream> #include <set> #include <map> #include <sstream> #include <typeinfo> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<string> vs; #define SIZE(x) ((int)(x.size())) #define LET(var, val) typeof(val) var = (val) #define FOR(k, a, b) for(typeof(a) k = (a); k < (b); ++k) #define FORR(k, a, b) for(typeof(a) k = (a); k >= (b); --k) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(LET(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define PF push_front const int INF = 2000000001; const double EPS = 10e-9; const double PI = acos(-1.0); ostringstream out; int objects[50]; int backpacks[150]; bool used[150]; int number_of_objects, number_of_backpacks; int best = 0; bool cmp(int a, int b) { return (a > b); } void func(int *used_capacity, int current_object, int used_backpacks) { if (current_object == number_of_objects) { if (used_backpacks < best) { best = used_backpacks; number_of_backpacks = best; } return; } REP(i, number_of_backpacks - 1) { if (used_capacity[i] + objects[current_object] <= backpacks[i]) { used_capacity[i] += objects[current_object]; func(used_capacity, current_object + 1, used_backpacks + (used_capacity[i] == objects[current_object] ? 1 : 0)); used_capacity[i] -= objects[current_object]; } } } int main() { ios_base::sync_with_stdio(0); cin >> number_of_objects >> number_of_backpacks; REP(i, number_of_objects) { cin >> objects[i]; used[i] = false; } int *capacity = new int[number_of_backpacks]; REP(i, number_of_backpacks) { cin >> backpacks[i]; capacity[i] = 0; } sort(backpacks, backpacks + number_of_backpacks, cmp); sort(objects, objects + number_of_objects, cmp); REP(i, number_of_backpacks) { REP(j, number_of_objects) { if (!used[j]) { if (capacity[i] + objects[j] <= backpacks[i]) { capacity[i] += objects[j]; if (capacity[i] == objects[j]) best++; used[j] = true; } } } } bool ready = false; REP(i, number_of_objects) if(!used[i]) { ready = true; break; } if (ready == true) best = INF; number_of_backpacks = min(best,number_of_backpacks); REP(i, number_of_backpacks) capacity[i] = 0; func(capacity, 0, 0); if (best == INF) cout << "NIE\n"; else cout << best << endl; }
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 | #include <algorithm> #include <string> #include <vector> #include <list> #include <cctype> #include <cmath> #include <iostream> #include <set> #include <map> #include <sstream> #include <typeinfo> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<string> vs; #define SIZE(x) ((int)(x.size())) #define LET(var, val) typeof(val) var = (val) #define FOR(k, a, b) for(typeof(a) k = (a); k < (b); ++k) #define FORR(k, a, b) for(typeof(a) k = (a); k >= (b); --k) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(LET(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define PF push_front const int INF = 2000000001; const double EPS = 10e-9; const double PI = acos(-1.0); ostringstream out; int objects[50]; int backpacks[150]; bool used[150]; int number_of_objects, number_of_backpacks; int best = 0; bool cmp(int a, int b) { return (a > b); } void func(int *used_capacity, int current_object, int used_backpacks) { if (current_object == number_of_objects) { if (used_backpacks < best) { best = used_backpacks; number_of_backpacks = best; } return; } REP(i, number_of_backpacks - 1) { if (used_capacity[i] + objects[current_object] <= backpacks[i]) { used_capacity[i] += objects[current_object]; func(used_capacity, current_object + 1, used_backpacks + (used_capacity[i] == objects[current_object] ? 1 : 0)); used_capacity[i] -= objects[current_object]; } } } int main() { ios_base::sync_with_stdio(0); cin >> number_of_objects >> number_of_backpacks; REP(i, number_of_objects) { cin >> objects[i]; used[i] = false; } int *capacity = new int[number_of_backpacks]; REP(i, number_of_backpacks) { cin >> backpacks[i]; capacity[i] = 0; } sort(backpacks, backpacks + number_of_backpacks, cmp); sort(objects, objects + number_of_objects, cmp); REP(i, number_of_backpacks) { REP(j, number_of_objects) { if (!used[j]) { if (capacity[i] + objects[j] <= backpacks[i]) { capacity[i] += objects[j]; if (capacity[i] == objects[j]) best++; used[j] = true; } } } } bool ready = false; REP(i, number_of_objects) if(!used[i]) { ready = true; break; } if (ready == true) best = INF; number_of_backpacks = min(best,number_of_backpacks); REP(i, number_of_backpacks) capacity[i] = 0; func(capacity, 0, 0); if (best == INF) cout << "NIE\n"; else cout << best << endl; } |