标签:
直接dp就可以啦~
设F[i][j]表示在第i首歌曲结束后,音量能否刚好为j
转移:F[i][j]=F[i][j-C[i]] or F[i][j+C[i]]
初始化:F[0][beginlevel]=true
最后在所有F[N][j]中找到F[N][j]为true的且要让j尽量大。如果所有0<=j<=maxlevel中的F[N][j]均为false,则输出-1。
#include <cstdio> using namespace std; const size_t Max_N(55); const size_t Max_Level(1050); size_t N; unsigned int Begin; unsigned int Level; unsigned int C; bool F[Max_N][Max_Level]; int main() { scanf("%u%u%u", &N, &Begin, &Level); F[0][Begin] = true; for (size_t i = 1;i <= N;++i) { scanf("%u", &C); for (unsigned int j = 0;j <= Level;++j) { if (j >= C) F[i][j] = F[i - 1][j - C]; if (j + C <= Level) F[i][j] = (F[i][j] || F[i - 1][j + C]); } } for (int j = Level;j >= 0;--j) if (F[N][j]) { printf("%d", j); return 0; } printf("-1"); return 0; }
标签:
原文地址:http://www.cnblogs.com/Created-equal/p/5021464.html