#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Sub
{
unsigned int sum;
int k;
Sub(unsigned int a, int b) : sum(a), k(b) {}
};
bool operator <(const Sub& s1, const Sub& s2)
{
return s1.sum < s2.sum;
}
void subset(unsigned int a[], const int& n, const unsigned int& max, int p, unsigned int sum, int x, vector<Sub>& subsum)
{
//cout << p << ' ';
if (p == n) {
if (sum <= max) subsum.push_back(Sub(sum, x));
}
else
{
subset(a, n, max, p+1, sum, x, subsum);
subset(a, n, max, p+1, sum+a[p], x | (1 << p), subsum);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m, i, j, count, k, full;
unsigned int a[24], c[100];
unsigned int total = 0, maxk, packs = 0;
vector<Sub> subs;
subs.reserve(16777216);
cin >> n >> m;
for (i = 0; i < n; ++i)
{
cin >> a[i];
total += a[i];
}
for (j = 0; j < m; ++j)
{
cin >> c[j];
//packs += c[j];
}
//if (total <= packs)
//{
sort(c, c+m);
maxk = c[m-1];
subset(a, n, maxk, 0, 0, 0, subs);
sort(subs.begin(), subs.end());
//cout << total << endl;
//for (vector<Sub>::iterator it = subs.begin(); it < subs.end(); ++it) cout << it->sum << ' ';
//cout << endl;
//for (vector<Sub>::iterator it = subs.begin(); it < subs.end(); ++it) cout << it->k << ' ';
//cout << endl;
//for (j = 0; j < m; ++j) cout << c[j] << ' ';
//cout << endl;
full = 0;
for (i = 0; i < n; ++i)
{
full <<= 1;
full |= 1;
}
i = subs.size() - 1;
j = m - 1;
count = 0;
k = 0;
while (i)
{
if (((subs[i].k & k) == 0) && subs[i].sum <= c[j])
{
k |= subs[i].k;
++count;
if (k == full) break;
--j;
}
--i;
}
//}
if (k == full) cout << count << '\n';
else cout << "NIE" << '\n';
}
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; struct Sub { unsigned int sum; int k; Sub(unsigned int a, int b) : sum(a), k(b) {} }; bool operator <(const Sub& s1, const Sub& s2) { return s1.sum < s2.sum; } void subset(unsigned int a[], const int& n, const unsigned int& max, int p, unsigned int sum, int x, vector<Sub>& subsum) { //cout << p << ' '; if (p == n) { if (sum <= max) subsum.push_back(Sub(sum, x)); } else { subset(a, n, max, p+1, sum, x, subsum); subset(a, n, max, p+1, sum+a[p], x | (1 << p), subsum); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, m, i, j, count, k, full; unsigned int a[24], c[100]; unsigned int total = 0, maxk, packs = 0; vector<Sub> subs; subs.reserve(16777216); cin >> n >> m; for (i = 0; i < n; ++i) { cin >> a[i]; total += a[i]; } for (j = 0; j < m; ++j) { cin >> c[j]; //packs += c[j]; } //if (total <= packs) //{ sort(c, c+m); maxk = c[m-1]; subset(a, n, maxk, 0, 0, 0, subs); sort(subs.begin(), subs.end()); //cout << total << endl; //for (vector<Sub>::iterator it = subs.begin(); it < subs.end(); ++it) cout << it->sum << ' '; //cout << endl; //for (vector<Sub>::iterator it = subs.begin(); it < subs.end(); ++it) cout << it->k << ' '; //cout << endl; //for (j = 0; j < m; ++j) cout << c[j] << ' '; //cout << endl; full = 0; for (i = 0; i < n; ++i) { full <<= 1; full |= 1; } i = subs.size() - 1; j = m - 1; count = 0; k = 0; while (i) { if (((subs[i].k & k) == 0) && subs[i].sum <= c[j]) { k |= subs[i].k; ++count; if (k == full) break; --j; } --i; } //} if (k == full) cout << count << '\n'; else cout << "NIE" << '\n'; } |
English