P1441 Weight Weighing

Topic description

There are n weights, the weights are a1, a2, a3, ..., an, after removing the m weights, ask how many different weights can be weighed at most (excluding 0).

Input and output format

Input format:

Line 1 of the input file weight.in has two integers n and m, separated by spaces

Line 2 has n positive integers a1, a2, a3, ..., an, representing the weight of each weight.

Output format:

The output file weight.out contains only 1 integer, which is the maximum weight that can be weighed.

Input and output example

Input Example #1: Copy
3 1
1 2 2
Output Sample #1: Copy
3

illustrate

【Example description】

After removing a weight with a weight of 2, a total of 3 weights 1, 2, and 3 can be weighed.

【Data scale】

For 20% of the data, m=0;

For 50% of the data, m≤1;

For 50% of the data, n≤10;

For 100% of the data, n≤20, m≤4, m<n, ai≤100.

 

Solution:

  This question compares water, first search and deal with the situation after deleting $m$ weights, then run $01$ backpack in the remaining $nm$ weights, process the weight that can be weighed, and update $ans$ $OK$.

Code:

 

 1 #include<bits/stdc++.h>
 2 #define il inline
 3 #define ll long long
 4 #define Max(a,b) (a)>(b)?(a):(b)
 5 #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
 6 #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
 7 using namespace std;
 8 const int N=105;
 9 int n,m,f[N*16],a[N],ans,sum;
10 bool vis[N];
11 il void solve(){
12     sum=0;memset(f,0,sizeof(f));
13     f[0]=1;
14     For(i,1,n)
15     if(!vis[i])Bor(j,a[i],1600)
16         if(f[j-a[i]]&&!f[j])sum++,f[j]=1;
17     ans=Max(ans,sum);
18 }
19 il void dfs(int s,int k){
20     if(!s){solve();return;}
21     For(i,k,n) if(!vis[i])vis[i]=1,dfs(s-1,i),vis[i]=0;
22 }
23 int main(){
24     cin>>n>>m;
25     For(i,1,n)cin>>a[i];
26     dfs(m,1);
27     cout<<ans;
28     return 0;
29 }

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325397577&siteId=291194637