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. SAFe必备——提高团队敏捷性

    规模化敏捷之于项目群,就像Scrum之于敏捷团队.为了创建高质量业务解决方案,企业需要提高自身能力,提升团队和技术敏捷性,实现真正的规模化敏捷. 敏捷发布火车 实现团队和技术敏捷性,首先需要敏捷团队围 ...

  2. AI顶会

    自己如何学习CV paper with code(每周) 基石方向 1.image classification 这个肯定要关注,这个往往都是backbone https://paperswithco ...

  3. shell脚本的常用执行方式

    1.sh+脚本的相对路径 [jinghang@hadoop101 datas]$ sh helloworld.sh helloworld sh+脚本的绝对路径 [jinghang@hadoop101 ...

  4. Dubbo系列之 (二)Registry注册中心-注册(1)

    引导 dubbo的服务的注册与发现,需要通过第三方注册中心来协助完成,目前dubbo支持的注册中心包括 zookeeper,consul,etcd3,eureka,nacas,redis,sofa.这 ...

  5. MyBatis-Pro,新一代的MyBatis增强框架

    框架功能 内置提供基础CRUD方法 提供根据方法名自进行单表查询(包括查询.统计.删除等) 接入方法 Spring Boot <dependency> <groupId>com ...

  6. Java indexof

    java中字符串方法 indexof() indexof()可以返回输入的字符串在目标字符串中第一次出现的位置,如果没有出现返回int 0:

  7. Go热门开源项目大全

    监控系统 项目 简介 Star数 grafana/grafana Grafana 是一个用于监控指标分析和图表展示的工具, 后端支持 Graphite, InfluxDB & Promethe ...

  8. 还不会springboot,阿里p8大牛一份385页pdf直接甩在脸上,给我啃

    第一章 Java EE简介 Java EE 有相应的规范实现,包括但不限于: Web 支持 事务支持 消息服务 数据库持久层 Container JWS JAX-RS JNDI JAXP/JAXB J ...

  9. Spark从入门到放弃---RDD

    什么是Spark? 关于Spark具体的定义,大家可以去阅读官网或者百度关于Spark的词条,在此不再赘述.从一个野生程序猿的角度去理解,作为大数据时代的一个准王者,Spark是一款主流的高性能分布式 ...

  10. Android app启动出现白屏闪屏

    出现白屏闪屏原因: 进入到AppStartActivity,但是未加载到布局文件,就先显示了窗口的背景,白屏就是显示的windows的背景,即所设置的theme. onCreate()中的setCon ...