标签:uva 12563 jin ge jin qu hao
首先这题的题意要先读懂,而不是一看到01背包的问题就开始敲。01背包的思路,每次记录下每个时间的最大歌曲数,最后找答案先满足歌曲数最大,再满足时间最大。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 55; const int MAX_TIME = 100005; int song[N],dp[MAX_TIME]; int n,t; int main() { int T,cas = 1; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&t); for(int i = 0; i < n; i++) { scanf("%d",&song[i]); } memset(dp,-1,sizeof(dp)); dp[0] = 0; for(int i = 0; i < n; i++) { for(int j = min(t-1,MAX_TIME) ; j >= song[i]; j--) { if(dp[j - song[i]] != -1) dp[j] = max(dp[j],dp[j-song[i]]+1); } } int maxn = 0, time; for(int i = MAX_TIME - 1; i >= 0; i--) { if(maxn < dp[i]+1) { maxn = dp[i]+1; time = i; } } printf("Case %d: %d %d\n",cas++,maxn,time + 678); } return 0; }
UVA - 12563 Jin Ge Jin Qu hao (01背包)
标签:uva 12563 jin ge jin qu hao
原文地址:http://blog.csdn.net/helloworld10086/article/details/42459871