第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)
链接:https://www.nowcoder.com/acm/contest/119/F
来源:牛客网
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the value of the tree. Also each of the trees have an area cost, it means we need to cost ci area of land to plant.
We know the cost and the value of all the trees. Now HUSTers want to maximize the value of trees which are planted in the land. Can you help them?
输入描述:
There are multiple cases.
The first line is an integer T(T≤10), which is the number of test cases.
For each test case, the first line is two number n(1≤n≤100) and C(1≤C≤10
8
), the number of seeds and the capacity of the land.
Then next n lines, each line contains two integer c
i
(1≤c
i
≤10
6
) and v
i
(1≤v
i
≤100), the space cost and the value of the i-th tree.
输出描述:
For each case, output one integer which means the max value of the trees that can be plant in the land.
输入例子:
1
3 10
5 10
5 10
4 12
输出例子:
22
-->
输入
1
3 10
5 10
5 10
4 12
输出
22
思路:这个01背包不是通常的01背包,因为背包容量太大,数组开不了那么大,于是就采用了换位思考(形容的不贴切)。开一个数组dp[i]一些物品价值为i时的最小质量。这样的话,最后求出质量小于等于背包总容量时的价值就是能放的最大价值。
注意:数组要初始化为无穷大,因为是求最小值,当然dp[0]还是要初始化为0的。
dp状态方程:dp[j]=min(dp[j],dp[j-weight[i]]+value[i]) j>=weight[i]。
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define MAXN 0x3f3f3f3f
using namespace std;
int dp[];
int value[];
int weight[];
int main(){
int n,w,i,j,sum;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&w);
sum=;
for(i=;i<=n;i++)
{
cin>>weight[i]>>value[i];
sum+=value[i];
}
for(i=;i<=sum;i++)dp[i]=MAXN;
dp[]=;
for(i=;i<=n;i++)
{
for(j=sum;j>=value[i];j--)
{
dp[j]=min(dp[j],dp[j-value[i]]+weight[i]);
}
}
for(i=sum;i>=;i--)
{
if(dp[i]<=w)
{
cout<<i<<endl;
break;
}
}
}
return ;
}
第十四届华中科技大学程序设计竞赛决赛同步赛 F Beautiful Land(01背包,背包体积超大时)的更多相关文章
- 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees
A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...
- 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...
- Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...
- 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】
题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...
- 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】
链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】
链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest
链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...
- 第十四届华中科技大学程序设计竞赛--J Various Tree
链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
随机推荐
- QQ空间如何设置被删除的好友不能访问空间
原来一直都很在乎.自己看着办. 经过简单的测试这种方法还是可以的啊,有问题到时候在说吧.理论上无论是否删除好友都可以限制对方访问空间. 旁边还有可以设置不能访问的名单(看仔细点-设置限制名单).
- IntelliJ Idea 快捷键精选
Alt+Enter,导入包,自动修正Ctrl+F,查找文本Alt+Up/Down,上/下移一行:在方法间快速移动定位:与光标位置有关Alt+Shift+Up,多次up可选中一行Ctrl+X,剪切行Ct ...
- WebUploader API文档
Web Uploader内部类的详细说明,以下提及的功能类,都可以在WebUploader这个变量中访问到. As you know, Web Uploader的每个文件都是用过AMD规范中的defi ...
- java深入探究12-框架之Hibernate
1.引入SSH框架 Struts框架,基于MVC 模式的应用层框架技术 Hibernate,基于持久层框架(数据访问层使用) Dao代码编写的几种方式: 1.原始jdbc操作,Connection/S ...
- alisql部署
参考文章:https://www.fooher.com/20170808_136.html 对于mysql部署,编译时需要哪些参考,为什么需要,过多的还有需要了解my.cnf配置文件的参数,以及不同环 ...
- Eclipse安装SVN客户端
在Eclipse中安装SVN客户端有个好处,不用兼容其它操作系统都能保持一致的操作.比如再Linux下SVN客户端软件体验相对较差,但是基于命令行的操作却在Linux下无所不能. 一.通过在线安装 地 ...
- 二叉查找树--java
package com.test.tree; public class BinarySearchTree<T extends Comparable<? super T>> { ...
- Spring Boot入门——json数据处理
1.引入fastJson插件 <!-- 引入fastjson插件 --> <dependency> <groupId>com.alibaba</groupId ...
- Redis学习路线
[http://www.cnblogs.com/zhenjing/archive/2012/11/15/redis_research.html] Redis: A persistent key-val ...
- WPF中如何在文本外面加虚线外框
WPF中如何在文本外面加虚线外框 昨天突然被问到如何在wpf里面给一段文本加个虚线外框,由于有一段时间没玩wpf了,一时还真没想出来,虽然大概有个思路,但是也不保证正确.今天回到家,闲着没事情也就随便 ...