UVa 10154 - Weights and Measures

本文针对UVa10154问题提供了一种解决方案,通过动态规划算法来确定最大数量的海龟堆叠方案。输入包括每只海龟的重量和承载力,输出为能稳定堆叠的最大海龟数量。

UVa 10154 - Weights 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.

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.

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

迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int f[6000];
struct ab
{
    int w,s;
} a[6000];
bool cmp(ab a,ab b)
{
    return a.s<b.s;
}
int main()
{
    int N=0;
    for(; ~scanf("%d%d",&a[N].w,&a[N].s); N++);
    sort(a,a+N,cmp);
    int M=0,i,j;
    memset(f,INF,sizeof(f));
    f[0]=0;
    for(i=0; i<N; i++)
        for(j=M; j>=0; j--)
        {
            if(a[i].s>=f[j]+a[i].w&&f[j]+a[i].w<f[j+1])
            {
                f[j+1]=f[j]+a[i].w;
                if(j+1>M) M=j+1;
            }
        }
    printf("%d\n",M);
    return 0;
}

转载于:https://www.cnblogs.com/im0qianqian/p/5989539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值