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. 畅购商城(八):微服务网关和JWT令牌

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...

  2. Web测试转App测试不看不知道

    Web测试 Web通常指的是互联网应用系统,比如税务电子化征管档案系统.金融数据平台.餐饮商家管理后台等等,其实质是C/S的程序. C是Client--客户端,S是Server--服务器. Web中的 ...

  3. Weighted-Residual-Connections

  4. 【模式识别与机器学习】——SVM举例

  5. 密码学系列——消息摘要(c#代码实操)

    前言 简介: 消息摘要(Message Digest)又称为数字摘要(Digital Digest) 它是一个唯一对应一个消息或文本的固定长度的值,它由一个单向Hash加密函数对消息进行作用而产生 使 ...

  6. JS学习第五天

    循环语句: for(变量 in (容器名)数组名){ 执行的语句块: break: 结束整个循环; continue:结束本次循环, 进入下一次循环: } 双层for循环: 外层循环控制行,内层循环控 ...

  7. 《Windows程序设计(第5版 珍藏版)》配书光盘

    https://pan.baidu.com/s/1ro72qQja_xTbf-Ik8b06Ng

  8. getting session bus failed: //bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

    今天在调试dbus程序的时候,运行程序出现了getting session bus failed: //bin/dbus-launch terminated abnormally with the f ...

  9. StructuredStreaming编程模型

    StructuredStreaming编程模型 基本概念 ◆ Time ◆ Trigger ◆ Input ◆ Query ◆ Result ◆ Output  案例模型:实时处理流单词统计编程模型 ...

  10. SpringBoot+MyBatis整合报错Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

    项目启动的时候报这个错误,这个问题我百度了一天,果然不出意外的还是没能解决,其中有一篇文章相对来说还是有点用的:https://blog.csdn.net/qq8693/article/details ...