#include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define pb push_back #define ll long long #define SZ(x) int((x).size()) int Prz[25]; unsigned int Sumy[18000000]; char Odp[18000000]; unsigned int Pod[18000000]; vector<unsigned int> Ple; int main () { int n,m; scanf("%d%d",&n,&m); FOR(i,n) scanf("%d", &Prz[i]); FORI(i,(1<<n)-1){ int x = (i-1)&i; int g = __builtin_ctz(i); Sumy[i] = Sumy[x]+Prz[g]; } FOR(i,m){ int a; scanf("%d",&a); Ple.pb(a); } Ple.pb(1000000000); sort(Ple.begin(),Ple.end(),greater<int>()); Ple[0] = 0; int COUNT = 0; FORI(i,(1<<n)-1){ int b = i; int kan = 1000; while(b != 0){ int g = __builtin_ctz(b); int G = (1<<g); if( Ple[Odp[i^G]+1] >= Prz[g] ) kan = min(kan,Odp[i^G]+1); if(Sumy[i^Pod[i^G]] <= Ple[Odp[i^G]]) kan = min(kan,(int)Odp[i^G]); if(kan == Odp[i^G]) break; b = (b-1) & b; } Odp[i] = kan; if(kan == 1000){ printf("NIE\n"); return 0; } b = i; int OP = 0; while(b != 0){ int g = __builtin_ctz(b); int G = (1<<g); if(Odp[i^G] == kan-1) {if(Sumy[OP] < Sumy[i^G]) OP = i^G;} else {if(Sumy[OP] < Sumy[Pod[i^G]]) OP = Pod[i^G];} b = (b-1) & b; } Pod[i] = OP; } printf("%d\n", (int)Odp[(1<<n)-1]); }
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 | #include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define REP(i,a,b) for(int i=(a);i<=(b);++i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define pb push_back #define ll long long #define SZ(x) int((x).size()) int Prz[25]; unsigned int Sumy[18000000]; char Odp[18000000]; unsigned int Pod[18000000]; vector<unsigned int> Ple; int main () { int n,m; scanf("%d%d",&n,&m); FOR(i,n) scanf("%d", &Prz[i]); FORI(i,(1<<n)-1){ int x = (i-1)&i; int g = __builtin_ctz(i); Sumy[i] = Sumy[x]+Prz[g]; } FOR(i,m){ int a; scanf("%d",&a); Ple.pb(a); } Ple.pb(1000000000); sort(Ple.begin(),Ple.end(),greater<int>()); Ple[0] = 0; int COUNT = 0; FORI(i,(1<<n)-1){ int b = i; int kan = 1000; while(b != 0){ int g = __builtin_ctz(b); int G = (1<<g); if( Ple[Odp[i^G]+1] >= Prz[g] ) kan = min(kan,Odp[i^G]+1); if(Sumy[i^Pod[i^G]] <= Ple[Odp[i^G]]) kan = min(kan,(int)Odp[i^G]); if(kan == Odp[i^G]) break; b = (b-1) & b; } Odp[i] = kan; if(kan == 1000){ printf("NIE\n"); return 0; } b = i; int OP = 0; while(b != 0){ int g = __builtin_ctz(b); int G = (1<<g); if(Odp[i^G] == kan-1) {if(Sumy[OP] < Sumy[i^G]) OP = i^G;} else {if(Sumy[OP] < Sumy[Pod[i^G]]) OP = Pod[i^G];} b = (b-1) & b; } Pod[i] = OP; } printf("%d\n", (int)Odp[(1<<n)-1]); } |