第十四届华中科技大学程序设计竞赛决赛同步赛 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 ...
随机推荐
- K8s API
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#daemonset-v1-apps http://docs.k ...
- Struts2的Action中访问servletAPI方式
struts2的数据存放中心为ActionContext,其是每次请求来时都会创建一个ActionContext,访问结束销毁,其绑定在ThreadLocal上,由于每次访问web容器都会为每次请求创 ...
- Variation calling and annotation
Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...
- 【P2964】硬币的游戏(DP+前缀和)
一道DP,思维难度真是不小. 首先对于这个题的数据,我们可以发现差不多可以支持n^2logn,但是貌似也不会有这种复杂度的线性DP(至少这个题看上去不是这样).所以我们考虑N^2做法.因为求得是价值和 ...
- jq点击切换按钮最简洁代码
<div id="swphoto"> <img src="1.jpg"> <img src="2.jpg&q ...
- VC 写注册表
BOOL Running() { HKEY hKey; LPCTSTR strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion ...
- Android 实现http通信(servlet做服务器端) HttpClient、HttpURLConnection实现登录验证
一,写好服务器端 在eclipse或其它javaee开发工具中新建一个web项目(我这里的项目名是:Android),建一个servlet(我这里的servlet名是:LoginServlet),模拟 ...
- selenium+python3 鼠标事件
1.鼠标右击 ActionChains(driver).contest_click(right_click).perform() 2.鼠标悬停 ActionChains(driver).move_to ...
- 配置JSP模板
- 偶尔出现button不能点击的情况
原因: 1.没有加点击事件 2.从别的项目或者别的场景导入的预制体,要更换canvas才能点击,不然没反应