HDOJ题目3496Watch The Movie(二维背包)

本文探讨了在有限时间内如何高效选择最喜爱的电影,以最大化观影体验与时间利用效率。通过实例分析,展示了如何在特定预算内选择电影,使得总观影价值最大化,满足观众在特定时间段内的娱乐需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Watch The Movie

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 5653    Accepted Submission(s): 1791


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
 

Source
 

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:   2955  2844  3033  2639  1712 
 
#include<stdio.h>
#include<string.h>
int dp[1010][110];
int main()
{
	int n,m,l;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int v[1010],t[1010],i,j,k;
		scanf("%d%d%d",&n,&m,&l);
		for(i=0;i<n;i++)
			scanf("%d%d",&t[i],&v[i]);
		memset(dp,0,sizeof(dp));
		for (j = 0; j <= l; ++j)
            for ( k = 0; k <= m; ++k)
                if (k == 0)
                    dp[j][k] = 0;
                else
                    dp[j][k] = -1e9;
				
				for(i=0;i<n;i++)
				{
					for(j=l;j>=t[i];j--)
					{
						for(k=1;k<=m;k++)
						{
							if(dp[j][k]<dp[j-t[i]][k-1]+v[i])
								dp[j][k]=dp[j-t[i]][k-1]+v[i];
						}
					}
				}
				if(dp[l][m]>0)
				printf("%d\n",dp[l][m]);
				else
					printf("0\n");
	}
}
还有看到了另一种思路转自 http://blog.youkuaiyun.com/zztant/article/details/17243611

有几个陷阱要注意

此题中对第一维的要求是必须选择M个物品,第二维的要求时,只要时间小于L都可以。

因此我们初始化dp,  memset(dp,0x80,sizeof(dp)),是一个非常小的负数。

在背包问题中有如下规定:

如果是要求背包恰好装满的最大值,那么初始化除了dp[0][0]=0,其他都为-inf 
如果没必要把背包装满,而只要求价值最大,那么直接初始化为0就可以了。

解的“连续性”决定了这个规则。

由于第二维不需要装满,因此只要在dp[M][j]中选择最大的那个即可。如果第二维必须装满,则只能选择dp[M][L]

ac代码
#include<iostream>
#include<cstring>
#define Max(a,b) (a<b?b:a)
#define Min(a,b) (a<b?a:b)
using namespace std;

int c_count;
int N,M,L;  
int t[101];
int val[101];
/*二维背包,设dp[k][i][j]为只使用前k种物品,选择其中i种物品,花费为j时的最大收益。*/
/*物品总个数限制and最大花费限制*/
/*且最后求的是背包第一维恰好满时的最大值*/
/*转移方程 dp[k][i][j]=Max(dp[k][i][j],dp[k-1][i-1][j-t[m]]+val[m])*/
int dp[101][1001];


int main(){
	cin>>c_count;
	while(c_count--){
		cin>>N>>M>>L;
		for(int i=0;i<N;i++){
			cin>>t[i]>>val[i];
		}
		memset(dp,0x80,sizeof(dp));
		dp[0][0]=0;
		for(int i=0;i<N;i++){
			for(int j=M;j>=1;j--){
				for(int k=L;k>=t[i];k--){
					dp[j][k]=Max(dp[j-1][k-t[i]]+val[i],dp[j][k]);
				}
			}
		}
		int ans=0;
		for(int i=L;i>=0;i--){
			if(dp[M][i]>ans){
				ans=dp[M][i];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值