链接: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. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...

  2. XML 的解析方法

    四种XML解析方法: (1)Dom生成和解析XML文档 *解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构.     * 优点:整个文档树在内存中,便 ...

  3. json01-json简介和语法

    JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法.类似 XML,但比 XML 更小.更快,更易解析,是轻量级的 ...

  4. 使用Navicat进行数据库自动备份

    今天经历一次数据库丢库事件,顿时觉得定时备份数据库很重要. 但是每天自己手动备份实在是太麻烦了,于是乎,想到用计划任务进行每天定时自动备份. 发现Navicat自带就有备份  还可以直接计划任务,贼方 ...

  5. 批量反编译class文件

    首先得进入jad的路径中,一般都放在jdk的安装目录的bin中 进入到该目录中,否则无法使用jad命令. jad -o -r -d F:\src -s java F:\classes\**\*.cla ...

  6. 只有*.mdf 如何附加数据库到MSSQL

        下载的webform 项目,App_Data文件夹中 只有*.mdf,无*.ldf日志文件. 直接在MSSQL企业管理中 附加数据库  提示附加失败. 新建一个与要附加的数据库同名的数据库,然 ...

  7. 分享知识-快乐自己:HashMap 与 HashTable 的区别

    特性: HashMap 与 Hashtable 的分析: 1):HashMap简介 1.底层数组+链表实现,可以存储null键和null值,线程不安全 2.HashMap 不是线程安全的 3.Hash ...

  8. hive中order by,sort by, distribute by, cluster by的用法

    1.order by hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...

  9. Django 基础 ORM系统

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  10. PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID

    向mysql 插入数据时,很多时候我们想知道刚刚插入数据的id,这对我们很有用.下面这篇文章就详细给大家介绍了利用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID,有需要的朋友 ...