有人称此题为双肩包,哈哈,因为题意是问你<=t-1时间内最多可以选择哪些歌曲使得 (数据1,数据2) 最优. 这里的数据1是歌曲数目, 数据2是歌曲总时长, 且数据1优先。
所以状态有两个并列的,并且优先顺序不一样。
我看了另外两篇博文,觉得dp写法很神奇,果然dp可以做多样变化,但是大同小异,殊途同归。
http://blog.csdn.net/u013480600/article/details/40376143
http://blog.csdn.net/u014733623/article/details/38169477
我这个写法也是借鉴了别人的,同样表示感谢。
刚学dp,想设计个状态转移真是费劲…………
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = int (1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
int dp[55][10000],len[55][10000];
int T[55];
int main() {
int m,n,t,k=0; cin>>m;
while (m--) {
memset (dp,0,sizeof (dp) );
memset (len,0,sizeof (len) );
scanf ("%d%d",&n,&t);
for (int i=1; i<=n; i++) {scanf ("%d",&T[i]);}
for (int i=n; i>=1; i--) {
for (int j=1; j<=t; j++) {
dp[i][j]=dp[i+1][j];
len[i][j]=len[i+1][j];
if (j>T[i]) {
if (dp[i][j]<dp[i+1][j-T[i]]+1) {
dp[i][j]=dp[i+1][j-T[i]]+1;//歌曲数目优先
len[i][j]=len[i+1][j-T[i]]+T[i];
} else if (dp[i][j]==dp[i+1][j-T[i]]+1) {
len[i][j]=max (len[i][j],len[i+1][j-T[i]]+T[i]);//时长其次
}
}
}
}
printf ("Case %d: %d %d\n",++k,dp[1][t]+1,len[1][t]+678);
}
return 0;
}