至今为止见过最恶心一道题。。。
思路是二维的01背包,这个只不过是多了一个限制条件,所以也就多了一层判断,状态方程也要变。大致懂了二维背包是什么了。。。
可是,初始化存在大问题,参考了n篇博客,初始化为-1的,我实在不能理解。
可是负无穷也明显是错的啊,题中明明写着She give a value Vi (Vi > 0) of the N piece of movies. V大于0啊!!!
还有题目的输出,If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.意思就是不能全部看完她叔叔买的录像才会输出0。我按照这种方式输出WA,看了别人的居然是总的喜欢值小于0输出0,这明显题不对口啊!
唯一一个解释应该就是题的问题了,代码放上来我也不知道怎么改了,如果有谁想通了留言吧
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 105;
const int INF = 1000000;
int ans, a[N], dp[N], pos[N];
int n, d;
int main()
{
// freopen("in.txt", "r", stdin);
int i, j, t, k, n, m, l;
int dp[N][1111], time[N], value[N];
scanf("%d", &t);
while(t --)
{
scanf("%d%d%d", &n, &m, &l);
for(i = 0; i < n; i ++)
scanf("%d%d",&time[i], &value[i]);
for(i = 0; i <= m; i ++)
{
for(j = 0; j <= l; j++)
{
if(i == 0)
dp[i][j] = 0;
else
dp[i][j] = - INF;//价值可能为负数
}
}
for(i = 0; i < n; i ++)
for(j = m; j >= 1; j --)
for(k = l; k >= time[i]; k --)//这里为何是time[i]截止而不是0截止
dp[j][k] = max(dp[j][k], dp[j - 1][k - time[i]] + value[i]);
if(dp[m][l] < 0)
dp[m][l] = 0;
printf("%d\n", dp[m][l]);
}
return 0;
}