HDU - 3496 Watch The Movie 【二维 0-1背包】

                                     Watch The Movie

                             Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

 

Problem Description

New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.
DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won’t stop until it goes to end.
But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L.
How clever you are! Please help DuoDuo’s uncle.

 

 

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:
The first line is: N(N <= 100),M(M<=N),L(L <= 1000)
N: the number of DVD that DuoDuo want buy.
M: the number of DVD that the shop can sale.
L: the longest time that her grandfather allowed to watch.
The second line to N+1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that DuoDuo rated.

 

 

Output

Contain one number. (It is less then 2^31.)
The total value that DuoDuo can get tonight.
If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.

 

Sample Input

1
3 2 10
11 100
1 2
9 1

Sample Output

3

题意

注意这题有个坑:最多看m部电影且必须看m部电影。

我们最终要求的是在时间L内看完M部电影能得到的最大“价值”,以Max[ j ][ m ]表示在是时间j内看完m部电影能得到的最大价值。

注意: 如果你在j时间内没看m部电影只看了t部(t < m),那么Max[ j ][ m ]就为负无穷,即使有可能只看t部电影所能得到的”价值“更大(为什么? 因为我们要求最大值,而你看不完m部就是不合要求,不合要求就不行,不行我让它为负无穷)。初始化Max数组为负无穷,Max[ j ][ 0 ] = 0.(还问为什么? 看0部电影啥也得不到”价值“不是0还能是啥?)

另外对m的枚举要从大到小且不能放在第一层循环。为什么???你想啊,我们用的状态递推式为Max[j][m] = max(Max[j][m],Max[j-c[i].c][m-1]+c[i].w),我只分析m位于第二层m从小打大为什么错误。 先分析m从小到大,假设我们算M最大为10,我们再算m=1的情况下,i=1(从下标0开始),j = 9时,当我们算到Max[10][1]的时候,Max[9][1] = max(Max[9][1],Max[9][0]+c[1].w) = 2,这是没问题的,然后m=2,当我们算到i=1,j=10的时候的时候,Max[10][2] =  max(Max[10][1],Max[9][1]+c[1].w) = 4,嘿嘿,问题来了,这里我们把第二个电影看了两次有没有,到那时每个只能看一次啊。其他的情况可以用下边代码中注释起来的输出试一下,一看就看出来。

AC代码

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct CD {
	int w,c;
}c[101];
int Max[1001][101];
const int INF = -0x3f3f3f3f;
int main()
{
	int t,N,M,L;
	cin >> t;
	
	while( t--)
	{
		cin >> N >> M >> L;
		for(int i=0;i<N;i++)
			cin >> c[i].c >> c[i].w;
		
		memset(Max,INF,sizeof Max);
		for(int i=0;i<=L;i++)
			Max[i][0] = 0;
		
		for(int i=0;i<N;i++)	
			for(int m=M;m>0;m--)		
				for(int j=L;j>=c[i].c;j--) {
				//	cout << "m = " << m << " i = " << i << " j = " << j << endl;
				//	cout << "Max[j][m] = " << Max[j][m] << " Max[j-c[i].c][m-1]+c[i].w = " << Max[j-c[i].c][m-1]+c[i].w << endl;
					Max[j][m] = max(Max[j][m],Max[j-c[i].c][m-1]+c[i].w);
				}	
		
		if(Max[L][M] < 0)
			Max[L][M] = 0;
		cout << endl; 
		cout << Max[L][M] << endl;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值