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. SQL性能优化-查询条件与字段分开执行,union代替in与or,存储过程代替union

    PS:概要.背景.结语都是日常“装X”,可以跳过直接看优化历程 环境:SQL Server 2008 R2.阿里云RDS:辅助工具:SQL 审计 概要 一个订单列表分页查询功能,单从SQL性能来讲,从 ...

  2. Springboot日志LOGO改变和设计

    每次启动Springboot的时候,SpringBoot都会打印一个LOGO,那么这个LOGO是可以关闭和改变的. 1.关闭Springboot的LOGO 2.改变Springboot的日志LOGO ...

  3. 使用Python语言通过PyQt5和socket实现UDP服务器

    前言 最近做了一个小软件,记录一下相关内容. 已有条件 现在已有一个硬件设备作为客户端(暂称其为"电路"). 基于SIM卡,电路可以通过UDP协议传输数据(程序已经内置在电路中), ...

  4. 打开终端自动source .bashrc文件

    vim ~/.bash_profile在文件内部输入 if test -f .bashrc ; then source .bashrc fi 就再也不用手动source了

  5. 2020-06-02:千万级数据量的list找一个数据。

    福哥答案2020-06-02: 对于千万级长度的数组单值查找:序号小的,单线程占明显优势:序号大的,多线程占明显优势.单线程时间不稳定,多线程时间稳定. go语言测试代码如下: package mai ...

  6. C#LeetCode刷题之#434-字符串中的单词数​​​​​​​(Number of Segments in a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3941 访问. 统计字符串中的单词个数,这里的单词指的是连续的不是 ...

  7. s2-001漏洞复现

    struts2-001 该漏洞因为用户提交表单数据并且验证失败时,后端会将用户之前提交的参数值使用 OGNL 表达式 %{value} 进行解析,然后重新填充到对应的表单数据中.例如注册或登录页面,提 ...

  8. docker基础入门理解

    本文简单的介绍了一下docker的一些优点,以及使用方法 1. 理解docker 1.1 docker是什么? 1.2 为什么要使用Docker? 2. docker安装 3. docker-容器,镜 ...

  9. Python+Pytest+Allure+Git+Jenkins接口自动化框架

    Python+Pytest+Allure+Git+Jenkins接口自动化框架 一.接口基础 接口测试是对系统和组件之间的接口进行测试,主要是效验数据的交换,传递和控制管理过程,以及相互逻辑依赖关系. ...

  10. ExcelWeb脚本助手,自定义脚本,批量操作Excel与网页

    ExcelWeb脚本助手,是一款可以自定义脚本操控Excel和浏览器的工具.提供了简单实用的Excel与Browser的API调用,通过自建脚本或自建项目,随意定制. 可以非常方便的根据Excel中的 ...