Bzoj 2748: [HAOI2012]音量调节
题目链接
比较水的一道DP题目.
设状态(f[i][j])表示第(i)首歌到达(j)音量是否可行.
CODE:
#include <iostream>
#include <cstdio>
const int maxN = 50 + 7;
const int maxL = 1000 + 7;
bool f[maxN][maxL];
int a[maxN];
int main() {
int n,begin,max_level;
scanf("%d%d%d",&n,&begin,&max_level);
f[0][begin] = true;
for(int i = 1;i <= n;++ i)
scanf("%d",&a[i]);//a[i]
for(int i = 1;i <= n;++ i) {
for(int j = 0;j <= max_level;++ j) {
if(j - a[i] >= 0 && f[i - 1][j - a[i]]) f[i][j] = true;
if(j + a[i] <= max_level && f[i - 1][j + a[i]]) f[i][j] = true;
}
}
for(int i = max_level;i >= 0 ; -- i)
if(f[n][i]) return printf("%d",i),0;
puts("-1");
return 0;
}