链接: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. python中IF语句容易犯的错误CASE

    python中没有switch   case类似的语句,但是下面的IF语句却与之类似,却又不同: A = B = C = D = E = 1 if A == 1: B=2 elif B ==2: C= ...

  2. Git安装/VScode+Git+Github

    Git安装/VScode+Git+Github 1. 相关简介 git 版本控制工具,支持该工具的网站有Github.BitBucket.Gitorious.国内的OS China仓库.Csdn仓库等 ...

  3. 转 10 jmeter之动态关联

    10 jmeter之动态关联   jmeter中关联是通过之前请求的后置处理器实现的,具体有两种方式:XPath Extractor(一般xml的时候用的多)和正则表达式提取器. 以webtours登 ...

  4. Vue中组件间通信的方式

    Vue中组件间通信的方式 Vue中组件间通信包括父子组件.兄弟组件.隔代组件之间通信. props $emit 这种组件通信的方式是我们运用的非常多的一种,props以单向数据流的形式可以很好的完成父 ...

  5. https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth

    https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth p.p ...

  6. java定时任务之Qutaz

    前不久一直在学习Qztaz,干好写了一个案例分享一下给大家,希望大家可以用得到. 那么现在开始吧, 一:什么事Qutaz? Quartz是OpenSymphony开源组织在Job scheduling ...

  7. Trie(字典树)

    没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...

  8. bzoj 2038(莫队算法)

    2038: [2009国家集训队]小Z的袜子(hose) 时间限制: 20 Sec  内存限制: 259 MB 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来 ...

  9. Docker是如何实现隔离的

    Docker是如何实现隔离的 2.进程的隔离 4.文件的隔离 5.资源的限制 7.与传统虚拟机技术的区别 原文地址: 微信公众号:<鲁智深菜园子>:Docker是如何实现隔离的 # 1.运 ...

  10. CSS选择器,属性前缀,长度单位,变形效果,过渡效果,动画效果

    CSS3选择器 ·*通配选择器 ·E标签选择器 ·E#id ID选择器 ·E.class类选择器 ·E F包含选择器,后代选择器 ·E>F子包含选择器 ·E+F相邻兄弟选择器 ·E[foo]属性 ...