TOJ3596 二维背包
2016-04-23 22:30
567 查看
3596. Watch The MovieTime Limit: 2.0 Seconds Memory Limit: 65536K
Total Runs: 424 Accepted Runs: 148
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.
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 i-th DVD, and the second number is the value of ith DVD that DuoDuo rated.
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.
Total Runs: 424 Accepted Runs: 148
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 i-th DVD, and the second number is the value of ith DVD that DuoDuo rated.
Output
Contain one number (It is less than 231).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 题目大意:多多要看影片,然后给她的看影片的时间是固定的,然后他的叔叔去挑选影片,一共有N张影片,他需要挑选M张, 每一张影片有两个属性,一个是快乐值,一个是放完需要的时间,问你如何挑选可以使多多的快乐值最大 。 思路分析:这是一道2维背包的题目,我们可以从挑选的要求来进行分析,首先每一张光盘有着耗时,这肯定是一种代价,但是 与以往的01背包问题进行对比,可以发现,以往的背包并没有限制物品的数量,但是这道题对物品的数量进行了限制,这就是另一个 代价,同时物品的数量要求必须达到M件,即恰好装满,所以在初始化的时候需要注意。 代码:#include <iostream> #include <stack> #include <cstdio> #include <cstring> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int f[105][1010]; int v[105],w[105]; #define inf 0x1f1f1f1f int main() { int t; scanf("%d",&t); while(t--) { int N,M,L; scanf("%d%d%d",&N,&M,&L); for(int i=1;i<=N;i++) { scanf("%d%d",&w[i],&v[i]); } memset(f,0,sizeof(f)); for(int i=1;i<=M;i++) { for(int j=0;j<=L;j++) { f[i][j]=-inf; } } for(int i=1;i<=N;i++) { for(int j=M;j>=1;j--) { for(int k=L;k>=w[i];k--) { f[j][k]=max(f[j-1][k-w[i]]+v[i],f[j][k]); } } } int ma=0; for(int i=1;i<=L;i++) if(ma<f[M][i]) ma=f[M][i]; cout<<ma<<endl; } }
相关文章推荐
- Hibernate注解----关联映射注解以及课程总结详解----图片版本
- [android] 手机卫士输入框抖动和手机震动
- 读取字体文件 模拟显卡点阵显示汉字
- 验证码生成工具类
- AndroidStudio中代码模板的使用
- 欧拉函数
- 深度优先搜索思想入门!
- 按天数计算兔子数量
- CentOS 下命令安装NetBeans
- 二维码生成工具类
- Android控件ListView
- 稀疏矩阵
- 电脑能ping通却打不开网页
- mybatis知识点总结和梳理
- python iterator generator yield
- 稳定排序与不稳定排序方法
- 使用RxJava优化Retrofit请求
- scikit-learn学习3.2.Grid Search:搜索估计器的参数
- HashMap的工作原理
- IE9 WIN7 卸载后解决安装的问题