Weights and Measures+uva+一般dp

探讨了如何通过动态规划解决乌龟堆叠问题,即寻找最大数量的乌龟能够稳定堆叠的方法。该问题涉及五千六百多只不同重量与承重能力的乌龟,目标是构建最高的稳定乌龟塔。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem F: 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
解决方案:想的时候竟然没想到当乌龟的重量会叠加,下面的乌龟就举不起来了。
还是看了别人的代码才把思路理清的,从只有一个乌龟开始,不断往上推,同时对叠加起来的乌龟的总重量进行更新,越轻越好。dp[j+1]=min{dp[j]+w[i]}&&(g[i]>dp[j]+w[i])边界条件:dp[1]=0;
code:
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
    int w,g;
} N[10000];
bool cmp(node a,node b)
{
    return a.g<b.g;
}
int dp[10000];
int main()
{
    int len=1;
    while(~scanf("%d%d",&N[len].w,&N[len].g))
    {
        len++;
    }
    sort(N+1,N+len+1,cmp);
    memset(dp,0x3f,sizeof(dp));
    dp[1]=0;
    int k=0;
    for(int i=1; i<len; i++)
    {

        for(int j=i-1; j>=1; j--)
        {
            if(dp[j]+N[i].w<N[i].g&&dp[j]+N[i].w<dp[j+1]){
                dp[j+1]=dp[j]+N[i].w;
                if(j>k) k=j;
            }
        }
    }
    printf("%d\n",k);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值