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;
}

最新文章

  1. TortoiseGit 连接oschina不用每次输入用户名和密码的方法
  2. $(function(){})、$(document).ready(function(){})....../ ready和onload的区别
  3. android开发 获取父控件的高宽
  4. jQuery支持mobile的全屏水平横向翻页效果
  5. some links
  6. virtualbox+centos 7 实现宿主机器互通
  7. web.xml中常用元素的解读
  8. Android破解学习之路(七)—— 乐秀视频编辑 内购破解 专业版 价值25元的破解
  9. Spark大型电商项目实战-及其改良(4) 单独运行程序发现的问题
  10. Win32汇编学习(5):绘制文本2
  11. [ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)]
  12. python线程和进程编程对比
  13. [转帖]Intel新一代Xeon完整曝光
  14. (DT系列五)Linux kernel 是怎么将 devicetree中的内容生成plateform_device【转】
  15. MongoDB入门一
  16. 使用Python计算IP、TCP、UDP校验和
  17. haproxy文章
  18. 利用Visio绘制数据流图与组织结构图
  19. [原]Failed to load SELinux policy. System Freezing ----redhat7or CentOS7 bug
  20. Mysql日期时间Extract函数介绍

热门文章

  1. 31-关键字:final
  2. vue中模块局部刷新
  3. “随手记”开发记录day05
  4. FPN和他的子孙们
  5. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
  6. scss @mixin &amp; @include
  7. Homekit_温湿度_人体红外_光强_传感器
  8. 使用 VMware Workstation Pro 让 PC 提供云桌面服务——学习笔记(二)
  9. (数据科学学习手札93)利用geopandas与PostGIS进行交互
  10. ANALYZE导致的阻塞问题分析