第十四届华中科技大学程序设计竞赛决赛同步赛 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 ...
随机推荐
- Centos 一次卸载多个RPM包
rpm 不支持通配符,可以使用 xargs来接收多个变量 示例,一次性卸载所有 erlang先关的rpm包: rpm -qa | grep erlang | xargs rpm -e --nodeps
- c++ boost库学习一:时间和日期
timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...
- unbunto关闭触摸屏
sudo rmmod psmouse 这个是禁用的 sudo modprobe psmouse 这个是启用的
- sem总结
从实况搜索这些关键词 有没有排名,有排名 不用管,没有的话 就一点点网上加价格 ,加到有为止 一个单元关键字控制30以内差不多 如果关键词词量有限 ,病种相差不大 可以不用屏蔽 投产=销量/消费订购率 ...
- 生产环境中,通过域名映射ip切换工具SwitchHosts
项目中,经常需要配置host.将某个域名指向某个ip.手动配置C:\Windows\System32\drivers\etc\hosts,非常不方便.这里分享一个可以高效切换host工具:Switch ...
- Codeforces Round #395 (Div. 2) C
题意 : 给出一颗树 每个点都有一个颜色 选一个点作为根节点 使它的子树各自纯色 我想到了缩点后check直径 当<=3的时候可能有解 12必定有解 3的时候需要check直径中点的组成点里是否 ...
- Log4j2的日志配置文件,log4j2.xml文件的配置(实现控制台输出,各级别分别文件输出,自动压缩等)
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W ...
- python 之CORS,VUE+rest_framework示例
一.跨域 浏览器的同源策略 ----对ajax请求进行阻拦 ----对href属性读不阻拦 xhr=new XML ...
- spring boot: 组合注解与元注解
package ch2.annotation; //target/elementType用来设定注解的使用范围 import java.lang.annotation.ElementType; imp ...
- php实现微信扫码自动登陆与注册功能
本文实例讲述了php实现微信扫码自动登陆与注册功能.分享给大家供大家参考,具体如下: 微信开发已经是现在程序员必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单 ...