uva10514Weights and Measures

 

 

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.

The Problem

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
动态规划经典题目;
第一种方法:
主要思想在于状态的选取上。
d[i][j],是指 第i只乌龟在处在j层时的最大力气。
由此可得出递推方程d[i][j]=max(min(d[i-1][j-1]-A[i].a,A[i].t),d[i-1][j]);          	
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct T
{
    int a,b,t;
} A[6000];
int d[6000][6000];
int cmp(const void *_a,const void *_b)
{
    T*a=(T*)_a;
    T*b=(T*)_b;
    if(a->t-b->t)return b->t-a->t;
}
int main()
{
   //freopen("1.txt","r",stdin);
    int n=0;
    while(~scanf("%d%d",&A[n].a,&A[n].b))
    {
        if(A[n].b>=A[n].a)
        {
            A[n].t=A[n].b-A[n].a;
            n++;
        }
    }
    qsort(A,n,sizeof(A[0]),cmp);
    memset(d,-1,sizeof(d));
    d[0][0]=A[0].t;
    int mm=0;
    for(int i=0; i<n; i++)
    for(int j=0; j<=mm; j++)
        {
            int ok=0;
            if(!j)
            {
                ok=1;
                if(i)d[i][j]=max(d[i-1][j],A[i].t);
            }
            else if(i)
            {
                int s=min(d[i-1][j-1]-A[i].a,A[i].t);
                if(d[i-1][j-1]-A[i].a>=0)ok=1;
                d[i][j]=max(s,d[i-1][j]);
            }
            if(ok)mm=max(j+1,mm);
        }
    printf("%d\n",mm);
    return 0;
}
第二种方法:
重点在于规划顺序上;
d[i]表示第一层时最大力气。
乌龟从大到小开始放置,更新状态从高层到下层更新。
得状态转移方程d[i]=max(min(d[i]-A[i].a,A[i].b),d[i]);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5700
using namespace std;
int d[N];
struct E
{
    int a,b;
} A[N];
bool cmp(E aa,E bb)
{
    return aa.b>bb.b;
}
int main()
{
    int n=0;
    while(~scanf("%d%d",&A[n].a,&A[n].b))
    {
        if(A[n].b>=A[n].a)A[n].b-=A[n++].a;
    }
    sort(A,A+n,cmp);
    memset(d,-1,sizeof(d));
    int mm=0;
    for(int i=0; i<n; i++)
    {
        int ok=0;
        for(int j=mm; j>=0; j--)
        {
            int s=A[i].b,o=0;
            if(j&&d[j-1]>=A[i].a)
            {
                o=1;
                if(j==mm)ok=1;
                s=min(d[j-1]-A[i].a,s);
            }
            if(!i||o)d[j]=max(s,d[j]);
        }
        if(ok||!i)mm++;
    }
    printf("%d\n",mm);
    return 0;
}
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值