#ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #include <functional> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; const int INF = 1000000001; const int N = 24; inline int Pow2(int exp) { return 1 << exp; } template<typename T> inline T Pow2(int exp) { return (T)1 << exp; } int Ctz(int x) { #ifdef __GNUC__ return __builtin_ctz(x); #else unsigned long r; _BitScanForward(&r, x); return r; #endif } struct Problem { int n,m; VI w,c; VI smw, minw; vector<vector<char> > rs; Problem(int n, int m) : n(n), m(m), w(n), c(m), smw(Pow2(n)), minw(Pow2(n)) { } int Go(); int Try(int ss, int mix); void GenSm(); }; int Problem::Go() { sort(ALL(c), greater<int>()); if (m > n) { c.resize(m); m = n; } rs.resize(m-1); REP(i,m-1) rs[i].resize(Pow2(n)); random_shuffle(ALL(w)); GenSm(); return Try(Pow2(n)-1, 0); } void Problem::GenSm() { int x = 0; LL s = 0; multiset<int> ws; FOR(i,1,Pow2(n)-1) { int p = Ctz(i); if (x & Pow2(p)) { s -= w[p]; multiset<int>::iterator it = ws.find(w[p]); ws.erase(it); } else { s += w[p]; ws.insert(w[p]); } x ^= Pow2(p); smw[x] = (int)min(s, (LL)INF); minw[x] = *ws.begin(); } } int Problem::Try(int ss, int mix) { if (smw[ss] <= c[mix]) return 1; if (mix == m-1) return 100; if (rs[mix][ss] != 0) return rs[mix][ss]; int v[N]; int k = 0; REP(i,n) { if (ss & Pow2(i)) v[k++] = i; } int x = 0; int res = 100; FOR(j,1,Pow2(k)-1) { int p = Ctz(j); x ^= Pow2(v[p]); if (smw[x] <= c[mix] && smw[x] + minw[ss^x] > c[mix]) { res = min(res, 1 + Try(ss ^ x, mix+1)); if (res == 2) break; } } rs[mix][ss] = res; return res; } int main(int argc, char** argv) { srand(time(0)); int n,m; scanf("%d%d", &n, &m); Problem p(n,m); REP(i,n) scanf("%d", &p.w[i]); REP(i,m) scanf("%d", &p.c[i]); int res = p.Go(); if (res == 100) printf("NIE\n"); else printf("%d\n", res); #ifdef _DEBUG system("pause"); #endif 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 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 | #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #else #define _CRT_SECURE_NO_WARNINGS #define _SCL_SECURE_NO_WARNINGS #endif #include <cstdlib> #include <ctime> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <algorithm> #include <string> #include <iostream> #include <functional> #define FOR(x,y,z) for (int x=(y); x<=(z); ++x) #define FORD(x,y,z) for(int x=(y); x>=(z); --x) #define REP(x,y) for (int x=0; x<(y); ++x) #if defined(__GNUC__) && __cplusplus < 201103L #define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y) #else #define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y) #endif #define ALL(x) (x).begin(),(x).end() #define SIZE(x) ((int)(x).size()) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; const int INF = 1000000001; const int N = 24; inline int Pow2(int exp) { return 1 << exp; } template<typename T> inline T Pow2(int exp) { return (T)1 << exp; } int Ctz(int x) { #ifdef __GNUC__ return __builtin_ctz(x); #else unsigned long r; _BitScanForward(&r, x); return r; #endif } struct Problem { int n,m; VI w,c; VI smw, minw; vector<vector<char> > rs; Problem(int n, int m) : n(n), m(m), w(n), c(m), smw(Pow2(n)), minw(Pow2(n)) { } int Go(); int Try(int ss, int mix); void GenSm(); }; int Problem::Go() { sort(ALL(c), greater<int>()); if (m > n) { c.resize(m); m = n; } rs.resize(m-1); REP(i,m-1) rs[i].resize(Pow2(n)); random_shuffle(ALL(w)); GenSm(); return Try(Pow2(n)-1, 0); } void Problem::GenSm() { int x = 0; LL s = 0; multiset<int> ws; FOR(i,1,Pow2(n)-1) { int p = Ctz(i); if (x & Pow2(p)) { s -= w[p]; multiset<int>::iterator it = ws.find(w[p]); ws.erase(it); } else { s += w[p]; ws.insert(w[p]); } x ^= Pow2(p); smw[x] = (int)min(s, (LL)INF); minw[x] = *ws.begin(); } } int Problem::Try(int ss, int mix) { if (smw[ss] <= c[mix]) return 1; if (mix == m-1) return 100; if (rs[mix][ss] != 0) return rs[mix][ss]; int v[N]; int k = 0; REP(i,n) { if (ss & Pow2(i)) v[k++] = i; } int x = 0; int res = 100; FOR(j,1,Pow2(k)-1) { int p = Ctz(j); x ^= Pow2(v[p]); if (smw[x] <= c[mix] && smw[x] + minw[ss^x] > c[mix]) { res = min(res, 1 + Try(ss ^ x, mix+1)); if (res == 2) break; } } rs[mix][ss] = res; return res; } int main(int argc, char** argv) { srand(time(0)); int n,m; scanf("%d%d", &n, &m); Problem p(n,m); REP(i,n) scanf("%d", &p.w[i]); REP(i,m) scanf("%d", &p.c[i]); int res = p.Go(); if (res == 100) printf("NIE\n"); else printf("%d\n", res); #ifdef _DEBUG system("pause"); #endif return 0; } |