Luo Gu P1877 [HAOI2012] volume control

Topic Portal

Problem-solving ideas:

F [i] [j] denotes the i-th song volume is reached whether j.

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 int n,b,v,f[51][2001],a[51];
 7 
 8 int main() {
 9     scanf("%d%d%d",&n,&b,&v);
10     for(int i = 1;i <= n; i++) {
11         scanf("%d",&a[i]);
12         f[0][b] = 1;
13     }
14     for(int i = 1;i <= n; i++)
15         for(int j = 0;j <= v; j++)
16             if(f[i-1][j])
17                 f[i][j+a[i]] = f[i][j-a[i]] = 1;
18     for(int i = v;i >= 0; i--) {
19         if(f[n][i]) {
20             printf("%d",i);
21             return 0;
22         }
23     }
24     printf("-1");
25     return 0;
26 }

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/12037425.html