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

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

输入

1
3 10
5 10
5 10
4 12

输出

22
这题如果硬算,v太大。
所以要转换下思维。
普通的01背包求的是相同c下最大的v
可以转换为相同v的情况下最小的c
这样就不会超时了。
 
 1 #include<cstdio>
2 #include<cstdlib>
3 #include<iostream>
4 #include<algorithm>
5 #include<cmath>
6 #include<cstring>
7 #include<map>
8 #define max(a,b)(a>b?a:b)
9 #define min(a,b)(a<b?a:b)
10 typedef long long ll;
11 using namespace std;
12 const int N = 10010;
13 const ll inf = 0x3f3f3f3f3f3f3f3f;
14 ll dp[N],w[N]; ///此时的dp[i]表示的是;价值为i时的最小容量为dp[i];
15 int v[N];
16
17 int main()
18 {
19 int T,i,n,sum;
20 ll V;
21 scanf("%d",&T);
22 while(T--)
23 {
24 scanf("%d%lld",&n,&V);
25 sum=0;
26 for(i=1;i<=n;i++)
27 {
28 scanf("%lld%d",&w[i],&v[i]);
29 sum=sum+v[i];
30 }
31
32 memset(dp,100000010,sizeof(dp)); ///要求最小容量,初始化为最大值;
33 dp[0]=0;
34 for(i=1;i<=n;i++)
35 {
36 for(int j=sum;j>=v[i];j--)
37 dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
38 }
39
40 for(i=sum;i>=0;i--)
41 {
42 if(dp[i]<=V)
43 {
44 printf("%d\n",i); ///此处输出i,即为满足条件的最大价值
45 break;
46 }
47 }
48 }
49 return 0;
50 }

牛客网-Beautiful Land 【01背包 + 思维】的更多相关文章

  1. 牛客网暑期ACM多校训练营(第三场) A PACM Team 01背包 记录路径

    链接:https://www.nowcoder.com/acm/contest/141/A来源:牛客网 Eddy was a contestant participating in ACM ICPC ...

  2. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

  3. 牛客网暑期ACM多校训练营(第四场) G Maximum Mode 思维

    链接:https://www.nowcoder.com/acm/contest/142/G来源:牛客网 The mode of an integer sequence is the value tha ...

  4. 牛客网第9场多校E(思维求期望)

    链接:https://www.nowcoder.com/acm/contest/147/E 来源:牛客网 题目描述 Niuniu likes to play OSU! We simplify the ...

  5. 2018牛客网暑期ACM多校训练营(第一场)B Symmetric Matrix(思维+数列递推)

    题意 给出一个矩阵,矩阵每行的和必须为2,且是一个主对称矩阵.问你大小为n的这样的合法矩阵有多少个. 分析 作者:美食不可负064链接:https://www.nowcoder.com/discuss ...

  6. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)

    链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...

  7. 牛客网 牛客练习赛13 C.幸运数字Ⅲ-思维

    C.幸运数字Ⅲ   链接:https://www.nowcoder.com/acm/contest/70/C来源:牛客网     这个题447和477是特殊的,其他的就没什么了.   代码: 1 #i ...

  8. 牛客网练习赛34-D-little w and Exchange(思维题)

    链接:https://ac.nowcoder.com/acm/contest/297/D 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

随机推荐

  1. Windows下的python虚拟环境设置

    Windows下的python虚拟环境设置: virtualenv 在python开发中,我们可能会遇到一种情况:就是当前的项目依赖的是某一个版本,但是另一个项目依赖的是另一个版本,这样就会造成依赖冲 ...

  2. apk开发环境!多亏这份《秋招+金九银十-腾讯面试题合集》跳槽薪资翻倍!再不刷题就晚了!

    开头 最近很多网友反馈:自己从各处弄来的资料,过于杂乱.零散.碎片化,看得时候觉得挺有用的,但过个半天,啥都记不起来了.其实,这就是缺少系统化学习的后果. 为了提高大家的学习效率,帮大家能快速掌握An ...

  3. ubuntu 更改U盘设备分区/dev/sdb4 标识

    备份u盘分区表 代码: sudo sfdisk -d /dev/sdb > sdb_table 修改sdb_table文件 代码: gedit sdb_table 恢复u盘分区表 代码: sud ...

  4. 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名。

    1 import org.apache.hadoop.conf.Configuration; 2 import org.apache.hadoop.fs.*; 3 import org.apache. ...

  5. 它真正的父进程在fork出子进程后就先于子进程exit退出了,所以它是一个由init继承的孤儿进程

    [Linux编程]守护进程(daemon)详解与创建_mick_seu的博客-CSDN博客_linux daemon https://blog.csdn.net/woxiaohahaa/article ...

  6. 在这个示例中,使用 watch 选项允许我们执行异步操作 (访问一个 API),限制我们执行该操作的频率,并在我们得到最终结果前,设置中间状态。这些都是计算属性无法做到的。

    在这个示例中,使用 watch 选项允许我们执行异步操作 (访问一个 API),限制我们执行该操作的频率,并在我们得到最终结果前,设置中间状态.这些都是计算属性无法做到的.

  7. python基础(数据类型,while,if)

    python基础初识. 1,运行python代码. 在d盘下创建一个t1.py文件内容是: print('hello world') 打开windows命令行输入cmd,确定后 写入代码python ...

  8. tarjan复习笔记 双连通分量,强连通分量

    声明:图自行参考割点和桥QVQ 双连通分量 如果一个无向连通图\(G=(V,E)\)中不存在割点(相对于这个图),则称它为点双连通图 如果一个无向连通图\(G=(V,E)\)中不存在割边(相对于这个图 ...

  9. PL/SQL 学习分享(续)

    事务 事务的概述 事务的特性 回滚点 事务实例练习 动态SQL 动态SQL概述 动态SQL应用场合 动态SQL的执行语法 绑定变量 动态SQL创建表 动态SQL绑定变量 动态SQL综合案例添加数据 使 ...

  10. SSH 登录警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    1.使用Linux SSH登录其他Linux或者cisco交换机时有如下报错 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! (远程主机标识已更改) ...