#include <cstring> #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 24; const unsigned int P = 1212121; unsigned int weight[N]; unsigned int capacity[110]; int n; int ile; int t[1<<N]; //liczba plecakow unsigned int poza[1<<N]; //waga w ostatnim plecaku int main(){ int m; scanf("%d %d", &n, &m); ile = 1<<n; REP(i,n) scanf("%u", weight+i); REP(i,m) scanf("%u", capacity+i); sort(capacity, capacity+m); reverse(capacity, capacity+m); REP(mask,ile) t[mask] = n+1; t[0] = 0; REP(mask,ile) if (t[mask] <= n) { unsigned int space = capacity[t[mask]] - poza[mask]; int a = t[mask]; int aa = a+1; unsigned int b = poza[mask]; REP(i,n){ int nmask = mask | (1<<i); if (weight[i] <= space) { if (t[nmask] > a || t[nmask] == a && poza[nmask] > b + weight[i]) { t[nmask] = a; poza[nmask] = b + weight[i]; } } else if (weight[i] <= capacity[t[mask]+1]) { if (t[nmask] > aa || t[nmask] == aa && poza[nmask] > weight[i]) { t[nmask] = aa; poza[nmask] = weight[i]; } } else if (nmask != mask) break; } t[mask]++; } if (t[ile-1] <= n) { printf("%d\n", t[ile-1]); } else { puts("NIE"); } 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 84 85 86 87 88 89 90 91 92 93 | #include <cstring> #include <cstdio> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <list> #include <queue> #include <deque> #include <cctype> #include <string> #include <vector> #include <sstream> #include <iterator> #include <numeric> #include <cmath> using namespace std; typedef vector <int > VI; typedef vector < VI > VVI; typedef long long LL; typedef vector < LL > VLL; typedef vector < double > VD; typedef vector < string > VS; typedef pair<int,int> PII; typedef vector <PII> VPII; typedef istringstream ISS; #define ALL(x) x.begin(),x.end() #define REP(i,n) for (int i=0; i<(n); ++i) #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var) #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it) #define PB push_back #define PF push_front #define MP(a,b) make_pair(a,b) #define ST first #define ND second #define SIZE(x) (int)x.size() const int N = 24; const unsigned int P = 1212121; unsigned int weight[N]; unsigned int capacity[110]; int n; int ile; int t[1<<N]; //liczba plecakow unsigned int poza[1<<N]; //waga w ostatnim plecaku int main(){ int m; scanf("%d %d", &n, &m); ile = 1<<n; REP(i,n) scanf("%u", weight+i); REP(i,m) scanf("%u", capacity+i); sort(capacity, capacity+m); reverse(capacity, capacity+m); REP(mask,ile) t[mask] = n+1; t[0] = 0; REP(mask,ile) if (t[mask] <= n) { unsigned int space = capacity[t[mask]] - poza[mask]; int a = t[mask]; int aa = a+1; unsigned int b = poza[mask]; REP(i,n){ int nmask = mask | (1<<i); if (weight[i] <= space) { if (t[nmask] > a || t[nmask] == a && poza[nmask] > b + weight[i]) { t[nmask] = a; poza[nmask] = b + weight[i]; } } else if (weight[i] <= capacity[t[mask]+1]) { if (t[nmask] > aa || t[nmask] == aa && poza[nmask] > weight[i]) { t[nmask] = aa; poza[nmask] = weight[i]; } } else if (nmask != mask) break; } t[mask]++; } if (t[ile-1] <= n) { printf("%d\n", t[ile-1]); } else { puts("NIE"); } return 0; } |