链接: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. 【小菜学网络】交换机与MAC地址学习

    上一小节介绍了 集线器 ,一种工作于物理层的简单网络设备.由于集线器采用广播的方式中继.转发物理信号,传输效率受到极大制约. 精准转发 为了解决集线器工作效率低下的尴尬,我们需要设计一种更高级的网络设 ...

  2. 2V升3.3V芯片,输出500MA,低功耗10uA解决方案

    2V的输入电压其实非常少,一般都是镍氢电池1.2V,干电池1.5V,来给玩具,MCU单片机,模块啊,等等供电.不过2V的供电电源或者设备确实是不常见的. 一般2V升3.3V,需要升压芯片PW5100即 ...

  3. uni-app开发经验分享三: Vuex实现登录和用户信息留存

    在做用户登录的过程中,其实最重要的是登录成功后的数据要怎么储存,储存到哪里,这里我分享一个利用vuex来实现用户登录和用户数据留存的方法 vuex代码如下: //引入vue和vuex import V ...

  4. 盼望着,盼望着。它终于来了!!!剪辑Windows PC测试版!

    盼望着,盼望着.它终于来了!!!剪辑Windows PC测试版! 喜欢短视频抖音的小伙伴们,是不是都对手机抖音剪映操作不是很满意.期待WINDOWS电脑版剪映的来临.在此之前只有MAC版本,不能满足广 ...

  5. vue3.0 composition API

    一.Setup函数 1.创建时间:组件创建之前被调用,优先与created被调用,this指向的实例为window,created所指向的实例为proxy 2.this指向:不会指向组件实例 3.参数 ...

  6. windows2012-2016亲测安装mysql8.0

    先去官网下载点击的MySQL的下载 下载完成后解压 解压完是这个样子 不要手动创建Data文件夹和my.ini文件, cmd命令窗口进入bin目录,如果已经做了环境变量那随意在哪里打开. mysqld ...

  7. echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转

    Echarts 标签中文本内容太长的时候怎么办 ? 关于这个问题搜索一下,有很多解决方案.无非就是 省略(间隔显示).旋转文字方向.竖排展示 前面两种解决方案,就是echarts暴露的: {   ax ...

  8. MySQL主从配置This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.

    MySQL主从配置This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD ...

  9. 变量隐藏Accidental Variable Shadowing

    6.5 - Variable shadowing (name hiding) | Learn C++ https://www.learncpp.com/cpp-tutorial/variable-sh ...

  10. shell脚本的使用该熟练起来了,你说呢?(篇一)

    作者:良知犹存 转载授权以及围观:欢迎添加微信公众号:羽林君