题解:砝码称重

大早上来机房刷试炼场块块

去上数学课之前过了八十分,原因出在边界的划分上

回来调了一下过了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int m, n, a[30];
 6 int ans=0, cnt, tot;
 7 bool used[30], f[2500];//方案数
 8 void work(){
 9     memset(f,0,sizeof(f));
10     f[0]=true;
11     cnt=0;
12     sum=0;
13     for(int i=1; i<=n; i++){
14         if(used[i]) continue;
15         for(int j=sum; j>=0; j--)
16             if(f[j] && !f[j+a[i]]){
17                 f[j+a[i]]=true;
18                 cnt++;
19             }              
20         sum+=a[i];
21     }
22     ans=max(ans,cnt);
23 }
24 void dfs(int now,int total){//已经扔出来的now个,现在是第total个,注意total包含扔了的和没扔的
25     if(now>m) return;
26     if(total==n+1){//注意边界
27         if(now==m) work();
28         return;
29     }
30     dfs(now, total+1);
31     used[total]=true;
32     dfs(now+1, total+1);
33     used[total]=false;
34 }
35 int main(){
36     cin>>n>>m;
37     for(int i=1; i<=n; i++){
38         cin>>a[i];
39     }
40     dfs(0,1);
41     cout<<ans<<endl;
42     return 0;
43 }

猜你喜欢

转载自www.cnblogs.com/Aze-qwq/p/9881222.html