#include <cstdio> #include <vector> #include <deque> #include <algorithm> #include <functional> #define N 25 using namespace std; typedef unsigned int uint; struct Item { Item(int id, uint price) : Id(id), Price(price) {} int Id; uint Price; }; struct Bag { Bag() {Reset(); } Bag(const Bag &a, const Bag &b, bool accumulate = false) { Reset(); for(int i=0;i<N;++i) Items[i] = a.Items[i] | b.Items[i]; Sum = a.Sum + b.Sum; if(accumulate) Accumulate = a.Accumulate + b.Accumulate; } bool IsFull(int n) { for(int i=1;i<=n;++i) if( !Items[i] ) return false; return true; } void AddItem(Item item) { Items[item.Id] = true; Sum += item.Price; } bool Intersects(const Bag &b) { for(int i=0;i<N;++i) if( b.Items[i] == true && Items[i] == true ) return true; return false; } inline bool operator<(const Bag &b) const { return Sum > b.Sum; } bool Items[N]; uint Sum; int Accumulate; void Reset() { for(int i=0;i<N;++i) Items[i] = 0; Sum = 0; Accumulate = 1; } }; void generateResults(vector<Bag> &r, int n, uint max); int analyzeResults(vector<Bag> &r, const vector<uint> &bags, int n, int minResult); int main() { int n, m, a, c, minResult = 0; long long sum = 0; vector<uint> bags; vector<Bag> results; scanf("%d%d", &n, &m); for(int i=1;i<=n;++i) { scanf("%d",&a); Item item(i,a); sum += a; Bag b; b.AddItem(item); results.push_back(b); } for(int i=0;i<m;++i) { scanf("%d", &c); bags.push_back(c); } sort(bags.begin(), bags.end(),std::greater<int>()); for(int i=0;i<m;++i) { if( sum > 0 ) { sum -= bags[i]; ++minResult; } } if( sum > 0 ) { puts("0"); return 0; } generateResults(results, n, bags[0]); sort(results.begin(), results.end()); int res = analyzeResults(results, bags, n, minResult); // for(int i=0;i<results.size();++i) // { // printf("%u ", results[i].Sum); // for(int j=0;j<N;++j) // if(results[i].Items[j] ) // printf("%d ",j); // puts(""); // } printf("%d\n", res); } void generateResults(vector<Bag> &r, int n, uint max) { for(int i=0;i<n;++i) { int last = r.size() - 1; for(int k = last; k > i ; --k) { if( (r[i].Sum + r[k].Sum) <= max && !r[i].Intersects(r[k]) ) { r.push_back(Bag(r[i], r[k])); } } } } int analyzeResults(vector<Bag> &r, const vector<uint> &bags, int n, int minResult) { deque<Bag> queue; int res = 1000000; while(!r.empty()) { queue.push_back(*r.begin()); r.erase(r.begin()); while(!queue.empty()) { Bag cur = queue.front(); queue.pop_front(); for(vector<Bag>::iterator iter = r.begin(); iter != r.end(); ++iter) { if( (*iter).Sum <= bags[cur.Accumulate] && !(*iter).Intersects(cur) ) { Bag b(*iter, cur, true); if( b.IsFull(n) ) { res = res < b.Accumulate ? res : b.Accumulate; if( res == minResult ) return minResult; } queue.push_back(b); } } } } return res; }
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <cstdio> #include <vector> #include <deque> #include <algorithm> #include <functional> #define N 25 using namespace std; typedef unsigned int uint; struct Item { Item(int id, uint price) : Id(id), Price(price) {} int Id; uint Price; }; struct Bag { Bag() {Reset(); } Bag(const Bag &a, const Bag &b, bool accumulate = false) { Reset(); for(int i=0;i<N;++i) Items[i] = a.Items[i] | b.Items[i]; Sum = a.Sum + b.Sum; if(accumulate) Accumulate = a.Accumulate + b.Accumulate; } bool IsFull(int n) { for(int i=1;i<=n;++i) if( !Items[i] ) return false; return true; } void AddItem(Item item) { Items[item.Id] = true; Sum += item.Price; } bool Intersects(const Bag &b) { for(int i=0;i<N;++i) if( b.Items[i] == true && Items[i] == true ) return true; return false; } inline bool operator<(const Bag &b) const { return Sum > b.Sum; } bool Items[N]; uint Sum; int Accumulate; void Reset() { for(int i=0;i<N;++i) Items[i] = 0; Sum = 0; Accumulate = 1; } }; void generateResults(vector<Bag> &r, int n, uint max); int analyzeResults(vector<Bag> &r, const vector<uint> &bags, int n, int minResult); int main() { int n, m, a, c, minResult = 0; long long sum = 0; vector<uint> bags; vector<Bag> results; scanf("%d%d", &n, &m); for(int i=1;i<=n;++i) { scanf("%d",&a); Item item(i,a); sum += a; Bag b; b.AddItem(item); results.push_back(b); } for(int i=0;i<m;++i) { scanf("%d", &c); bags.push_back(c); } sort(bags.begin(), bags.end(),std::greater<int>()); for(int i=0;i<m;++i) { if( sum > 0 ) { sum -= bags[i]; ++minResult; } } if( sum > 0 ) { puts("0"); return 0; } generateResults(results, n, bags[0]); sort(results.begin(), results.end()); int res = analyzeResults(results, bags, n, minResult); // for(int i=0;i<results.size();++i) // { // printf("%u ", results[i].Sum); // for(int j=0;j<N;++j) // if(results[i].Items[j] ) // printf("%d ",j); // puts(""); // } printf("%d\n", res); } void generateResults(vector<Bag> &r, int n, uint max) { for(int i=0;i<n;++i) { int last = r.size() - 1; for(int k = last; k > i ; --k) { if( (r[i].Sum + r[k].Sum) <= max && !r[i].Intersects(r[k]) ) { r.push_back(Bag(r[i], r[k])); } } } } int analyzeResults(vector<Bag> &r, const vector<uint> &bags, int n, int minResult) { deque<Bag> queue; int res = 1000000; while(!r.empty()) { queue.push_back(*r.begin()); r.erase(r.begin()); while(!queue.empty()) { Bag cur = queue.front(); queue.pop_front(); for(vector<Bag>::iterator iter = r.begin(); iter != r.end(); ++iter) { if( (*iter).Sum <= bags[cur.Accumulate] && !(*iter).Intersects(cur) ) { Bag b(*iter, cur, true); if( b.IsFull(n) ) { res = res < b.Accumulate ? res : b.Accumulate; if( res == minResult ) return minResult; } queue.push_back(b); } } } } return res; } |