I know, up on top you are seeing great sights,

But down at the bottom, we, too, should have rights.

We turtles can’t stand it. Our shells will all crack!

Besides, we need food. We are starving!” groaned Mack.

Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles

should be dispatched to form Yertle’s throne. Each of the five thousand, six hundred and seven turtles

ordered by Yertle has a different weight and strength. Your task is to build the largest stack of turtles

possible.

Input

Standard input consists of several lines, each containing a pair of integers separated by one or more

space characters, specifying the weight and strength of a turtle. The weight of the turtle is in grams.

The strength, also in grams, is the turtle’s overall carrying capacity, including its own weight. That is,

a turtle weighing 300g with a strength of 1000g could carry 700g of turtles on its back. There are at

most 5,607 turtles.

Output

Your output is a single integer indicating the maximum number of turtles that can be stacked without

exceeding the strength of any one.

Sample Input

300 1000

1000 1200

200 600

100 101

Sample Output

3

思路:先按照载重从小到大排序,然后dp即可,状态转移方程dp[t][j] = min(dp[t][j], dp[t-1][j-1] +p[t].a)。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std; struct node {
int a,b; } p[10005];
long long int dp[10005][10005];
bool cmp(node x,node y)
{
return x.b<y.b;
}
int main() {
int n=1;
while(scanf("%d%d",&p[n].a,&p[n].b)!=EOF) {
n++;
}
n--;
for(int t=0;t<=n;t++)
{
for(int j=1;j<=n;j++)
{
dp[t][j]=INF;
}
}
sort(p+1,p+n+1,cmp);
for (int t = 1; t <= n; t ++)
for (int j = 1; j <= t; j ++) {
dp[t][j] = dp[t-1][j];
if (dp[t-1][j - 1] + p[t].a <= p[t].b && dp[t-1][j-1] !=INF)
dp[t][j] = min(dp[t][j], dp[t-1][j-1] +p[t].a);
}
int flag;
for(int k=n;k>=1;k--)
{
if(dp[n][k]!=INF)
{
flag=k;
break;
}
}
printf("%d\n",flag);
return 0;
}

Weights and Measures (贪心+dp)的更多相关文章

  1. uva 10154 - Weights and Measures【dp】qi

    题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包含自己的所有的重量. 分析:先按其能举起来的力量从小 ...

  2. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  3. UVa 10154 - Weights and Measures

    UVa 10154 - Weights and Measures I know, up on top you are seeing great sights,  But down at the bot ...

  4. BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP

    BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀 ...

  5. 洛谷P4823 拯救小矮人 [TJOI2013] 贪心+dp

    正解:贪心+dp 解题报告: 传送门! 我以前好像碰到过这题的说,,,有可能是做过类似的题qwq? 首先考虑这种显然是dp?就f[i][j]:决策到了地i个人,跑了j个的最大高度,不断更新j的上限就得 ...

  6. 【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp

    题目描述 给出 $A$ 串和 $B$ 串,从 $A$ 串中选出至多 $x$ 个互不重合的段,使得它们按照原顺序拼接后能够得到 $B$ 串.求是否可行.多组数据. $T\le 10$ ,$|A|,|B| ...

  7. 【bzoj3174】[Tjoi2013]拯救小矮人 贪心+dp

    题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以碰到陷阱口.对于每一个小矮人,我们知道他从脚 ...

  8. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. 贪心+DP【洛谷P4823】 [TJOI2013]拯救小矮人

    P4823 [TJOI2013]拯救小矮人 题目描述 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀上,知道最顶端的小矮人伸直胳膊可以 ...

随机推荐

  1. 图计算实现ID_Mapping、Oneid打通数据孤岛

    图计算实现ID_Mapping.Oneid打通数据孤岛 ID_Mapping与Oneid的作用 大神告诉我们Oneid能用来做什么 输入数据源格式样例 实现原理 当日代码生成 引用jar包 启动命令 ...

  2. java web应用启动报错:Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already in use.

    Several ports (8080, 8009) required by Tomcat v6.0 Server at localhost are already in use. The serve ...

  3. geth常用命令

    转载地址 https://blog.csdn.net/qq_36124194/article/details/83686823 geth常用命令 初始化私链 geth --datadir /path/ ...

  4. # c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符

    c++运算符重载之 前置++, 后置++, 负号运算符, 类型转换函数, 以及输入输出运算符 标签(空格分隔): c++ 前言 我在c++学习的过程中, 对这几个不太常见的运算符重载不太会写.出现了很 ...

  5. [源码分析]ArrayList和LinkedList如何实现的?我看你还有机会!

    文章已经收录在 Github.com/niumoo/JavaNotes ,更有 Java 程序员所需要掌握的核心知识,欢迎Star和指教. 欢迎关注我的公众号,文章每周更新. 前言 说真的,在 Jav ...

  6. Maven报错Missing artifact jdk.tools:jdk.tools:jar:1.7

    1.eclipse中Maven项目的pom文件报错: 2.解决方法: 直接在pom.xml中加上一个依赖项目: <dependency>      <groupId>jdk.t ...

  7. 01@-tornado

    import tornado.web ''' tornado的基础web框架模块 ''' import tornado.ioloop ''' tornado的核心IO循环模块 封装了Linux的epo ...

  8. phpstudy后门利用复现

    一.漏洞位置 程序自带的PHP的php_xmlrpc.dll模块中有隐藏后门,受影响的版本有phpstudy2016(php5.2/5.4).phpstudy2018(php5.2/5.4)等版本. ...

  9. springMVC入门(七)------RESTFul风格的支持

    简介 RESTful风格(Representational State Transfer),又叫表现层状态转移,是一种开发理念,也是对HTTP协议很好的诠释 主要理念是将互联网中的网页.数据.服务都视 ...

  10. WSGI 配置禁止反向DNS查找

    原文链接:https://opendev.org/starlingx/ha/src/commit/045a37c672a92f1412629a176f51183c88882e61/service-mg ...