链接: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. 与数论的厮守05:gcd(a,b)=gcd(b,a mod b)的证明

    \[设c=gcd(a,b),那么a可以表示为mc,b可以表示为nc的形式.然后令a=kb+r,那么我们就\\ 只需要证明gcd(b,r)=c即可.{\because}r=a-kb=mc-knc,{\t ...

  2. 【高并发】ReadWriteLock怎么和缓存扯上关系了?!

    写在前面 在实际工作中,有一种非常普遍的并发场景:那就是读多写少的场景.在这种场景下,为了优化程序的性能,我们经常使用缓存来提高应用的访问性能.因为缓存非常适合使用在读多写少的场景中.而在并发场景中, ...

  3. 阿里云OSS对象存储服务(二)

    一.使用SDK 在OSS的概览页右下角找到"Bucket管理",点击"OSS学习路径" 点击"Java SDK"进入SDK开发文档 二.创建 ...

  4. Django orm中related_name/related_query_name区别

    related_name/related_query_name区别 class Department(models.Model): title = models.CharField(verbose_n ...

  5. 04. struts2中Result配置的各种视图转发类型

    概述 <action name="helloworld" class="com.liuyong666.action.HelloWorldAction"&g ...

  6. Spring基于注解开发的注解使用之AOP(部分源代码分析)

    AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...

  7. hive搜索报错

    在自己搭建的集群上执行hive搜索语句 select count(*) from ods_event_log where dt='2019-12-14' group by dt; 报错如下: Stat ...

  8. 页面渲染html的过程

    浏览器渲染页面的一般过程: 1.浏览器解析html源码,然后创建一个 DOM树.并行请求 css/image/js在DOM树中,每一个HTML标签都有一个对应的节点,并且每一个文本也都会有一个对应的文 ...

  9. Codeforces 1220D 思维 数学 二分图基础

    原题链接 题意 我们有一个含多个正整数的集合B,然后我们将所有的整数,也就是Z集合内所有元素,都当做顶点 两个整数 \(i , j\) 能建立无向边,当且仅当 \(|i - j|\) 这个数属于B集合 ...

  10. SparkMLlib—协同过滤之交替最小二乘法ALS原理与实践

    SparkMLlib-协同过滤之交替最小二乘法ALS原理与实践 一.Spark MLlib算法实现 1.1 显示反馈 1.1.1 基于RDD 1.1.2 基于DataFrame 1.2 隐式反馈 二. ...