链接:https://www.nowcoder.com/acm/contest/119/F
来源:牛客网

Beautiful Land
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
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

输入

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背包,背包体积超大时)的更多相关文章

  1. 第十四届华中科技大学程序设计竞赛决赛同步赛 A - Beauty of Trees

    A - Beauty of Trees 题意: 链接:https://www.nowcoder.com/acm/contest/119/A来源:牛客网 Beauty of Trees 时间限制:C/C ...

  2. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  3. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

  4. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  5. 第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

    链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  6. 第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

    链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  7. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  8. 第十四届华中科技大学程序设计竞赛 K--Walking in the Forest

    链接:https://www.nowcoder.com/acm/contest/106/K来源:牛客网 题目描述 It’s universally acknowledged that there’re ...

  9. 第十四届华中科技大学程序设计竞赛--J Various Tree

    链接:https://www.nowcoder.com/acm/contest/106/J来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

随机推荐

  1. linux 虚拟机在线添加新磁盘

    在线添加磁盘,扩展LVM卷案例   一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...

  2. .on事件绑定多少次就会执行多少次

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 用SQL语句删除除了id不同,其他都相同的学生表信息

    delete from <table_name> wehere id not in (select max(id) from <table_name> group by < ...

  4. list!=null跟list.isEmpty()有什么区别?

    这就相当与,你要喝水,前面list!=null就是判断是不是连水杯都没有,后面!list.isEmpty就是判断水杯里面没有水,连盛水的东西都没有,这个水从何而来?所以一般的判断是if(list!=n ...

  5. java常见设计模式

    工厂模式 普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建. 多个工厂模式,编写多个创建工厂的方法即可. 静态工厂模式,在多个工厂模式的基础上把Factory种方法的返回值标明 ...

  6. review01

    .java叫源文件,java编译器编译源文件后会产生字节码文件,java解释器将字节码文件加载进内存,java虚拟机来执行字节码文件. 如下列文件名为“String01.java” public cl ...

  7. MDX中Filter 与Exist的区别

        获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...

  8. 2017-02-20 Sql Server2016安装后无法找到Microsoft Sql Server Management Studio管理器

    最近安装的sql sever2016后发现没有Sql server management studio管理工具,无法操作sql server 解决方案,可去官网单独下载 Sql Server Mana ...

  9. EF4 Model 代码生成EFPowerTools,Vs2015不支持,自己封装一个

    Vs2013上面有个插件EFPowerTools,用来生产entity实体代码的.目前官方只支持到Vs2013,虽然经过网上网友的方法,修改支持版本号,可以在Vs2015上安装.安装后连接远程数据库还 ...

  10. Mssql 跨域查询

    有数据库test1和数据库test2.其中test1中有表 table1.table2:test2 中有表 table1.三个表的字段都为为:id.xingming.shijian.shuliang. ...