砝码称重

60'

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<bitset>
 5 using namespace std;
 6 const int maxn=27;
 7 const int maxm=2007;
 8 bitset<21>bt;
 9 int n,m,ans,s;
10 int a[maxn],f[maxn][maxm][3];
11 bool vis[maxm];
12 void cunt(int x,int sum,int opt){ 
13     if(f[x][sum][opt]==1) return; 
14     if(x==n){
15         vis[sum]=true;
16         return;
17     }
18     if(opt==0){
19         if(bt[x+1]!=1) cunt(x+1,sum,1);
20         cunt(x+1,sum,0);
21     }
22     else{
23         if(bt[x+1]!=1) cunt(x+1,sum+a[x+1],1);
24         cunt(x+1,sum+a[x+1],0);
25     }
26     f[x][sum][opt]=1;
27     return;
28 }
29 void prt(int x){
30     if(x==n+1&&bt.count()==m){
31         memset(vis,false,sizeof(vis));
32         memset(f,0,sizeof(f));
33         cunt(0,0,0);
34         if(bt[0]!=1) cunt(0,0,1);
35         int t=0;
36         for(int i=1;i<=s;i++) if(vis[i]==true) t++; 
37         ans=max(ans,t);
38         return;
39     }
40     if(bt.count()>m) return;
41     if(x==n+1&&bt.count()!=m) return;
42     prt(x+1);
43     bt.set(x-1,1);
44     prt(x+1);
45     bt.set(x-1,0);
46     return;
47 }
48 int main(){
49     cin>>n>>m;
50     for(int i=1;i<=n;i++){cin>>a[i];s+=a[i];}
51     for(int i=1;i<=n;i++) prt(i);
52     cout<<ans<<endl;
53     return 0;
54 }

100'

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<bitset>
 4 using namespace std;
 5 int n,m,ans;
 6 int val[27];
 7 int count(int x){
 8     int cnt=0;
 9     for(int i=0;i<=n-1;i++){
10         if(x&(1<<i)) cnt++;
11     } 
12     return cnt;
13 }
14 int main(){
15     cin>>n>>m;
16     for(int i=0;i<n;i++) cin>>val[i];
17     for(int i=0;i<=(1<<n)-1;i++){
18         if(count(i)==n-m){
19             bitset<2007>bt;
20             bt[0]=1;
21             for(int j=0;j<=(n-1);j++){
22                 if(i&(1<<j)) bt=bt|(bt<<val[j]);
23             }
24             ans=max(ans,(int)bt.count());
25         }
26     }
27     cout<<ans-1<<endl;
28     return 0;
29 }

 

posted @ 2018-08-30 20:54  lcan  阅读(195)  评论(0编辑  收藏  举报