uva10154 - Weights and Measures

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

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

 

  每个乌龟有自己的重量和力量,力量就是它能背的重量(包括它自己),问最多能叠多少个乌龟。

  这道题百度说要先按照力量排序,仔细想一下,确实这样能使乌龟尽可能多。假设两个乌龟的重量和力量分别是w1,s1,w2,s2,s1<s2,假设乌龟1在下面,可以背min(s1-w1-w2,s2-w2)=s1-w1-w2,乌龟2在下面可以背min(s2-w1-w2,s1-w1)>s1-w1-w2。所以把力量大的放下面好。

  开始总想着怎么在顶上加乌龟最优,但是又不知道下面的乌龟能背多少,但是如果考虑在底下加乌龟,只需要它的力量比上面那些乌龟加上它自己的重量大就行了。用f[i]表示能堆i个乌龟它们的最小重量。这个问题就有点像01背包(01背包放物品顺序不影响结果,这道题要先排序,否则就可能处理完第i个乌龟时没有得到最优的f,也就不满足最优子结构)。a[i]是第i个乌龟,M是目前能堆的最大数目,于是

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

完整代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int f[6000];
struct turtle{
    int w,s;
}a[6000];
bool cmp(turtle a,turtle b){
    return a.s<b.s;
}
int main(){
    freopen("in.txt","r",stdin);
    int N=0;
    while(scanf("%d%d",&a[N].w,&a[N].s)!=EOF) 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++)            //第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;
}




uva 10154 Weights and Measures 题目链接 题意:有若干只乌龟n~10000,每只乌龟有重量和负重两个属性,现在求这若干只乌龟最多可以叠多少层 题解:乌龟的排列顺序会影响到结果,所以第一步是排序贪心。标准以前的题目中写过,就是用两只乌龟比较。然后dp【i】,指叠放了i只乌龟,然后最薄弱的承受是多少。 重点:排序dp #include #include #include #include #include #inc 阅读详情

相关推荐

UVA10154Weights and Measures(类01背包)

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 starv...

weixin_33696106的博客 172

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!” groan...

starlet_kiss的博客 336

UVA 10154 Weights and Measures

做题感悟:这题开始用力量减去自身体重

Muti-Agent 2767

UVA 10154 Weights and Measures 01背包

题意:给出n只乌龟的体重和能承受的重量,它自己的体重也要承受,要把乌龟们叠起来,问最多能叠到多高。 其实就是变形的01背包,先把乌龟按力量排序,然后再进行01背包。 代码: /* * Author: illuz * Blog: http://blog.csdn.net/hcbbt * File: uva10154.cpp * Create

水果君の日常 2678

Weights and Measures——UVA10154

 更详细见http://www.cnblogs.com/staginner/archive/2011/11/30/2268497.html 题目 Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched t...

baidu_41907100的博客 228

uva 10154 - Weights and Measures【dp】qi

题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度、力量必须承受其上面包括自己的全部的重量。 分析:先按力量从小到大排序 然后定义dp【i】 表示摞起来 i 只乌龟的最小质量。 然后转移就是每次用遍历O(n)的负责度找最小的,然后记录,保存最大值即可。 AC代码: #include #i

大神养成中..... 1260

UVa Problem 10154 Weights and Measures (重量和力量)

// Weights and Measures (重量和力量) // PC/UVa IDs: 111103/10154, Popularity: C, Success rate: average Level: 3 // Verdict: Programming Challenges - Solved, UVa - Accepted // Submission Date: 2011-10-12 //

寂静山林 3673

UVa:10154 Weights and Measures

一度把题目想简单,WA1次。后来没有想清楚第i只龟不能承受的情况又WA了一次。   如果用一维数组保存状态的话,过程是不满足无后效性的(这里我居然想到了),所以需要加维来消去后效性。 想到了两种加维的办法。 1.dp【i】【j】表示第i只龟承重为j时所载最大龟数。       质量上限j题目并未给出,估计很大,数组很难存下。 2.dp【i】【j】表示第i只龟承载j只龟时所承受的最小质量。

Fighting 976

UVa 10154. Weights and Measures

题意:有几只乌龟,每只乌龟有一定的重量与力量。每只乌龟可以背小于它力量的重量(包括它自身的重量)。问最多一共可以有多少只乌龟叠在一起。 可以证明,将力量大的乌龟放在下面可以得到一个更优的状态。因此在dp之前应先将所有乌龟按力量大小排好序,力量小的在前面。(证明可见这个网页) 1.用dp[i][j]表示从前 i 只乌龟中选出 j 只可以得打的最小总重量。 转移方程为:如果dp[i-1][j-1

Just for fun 1658

UVA10154 Weights and Measures【0-1背包】

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 a

海岛Blog 446

UVA10154 Weights and Measures

https://vjudge.net/problem/UVA-10154 ↑Vjudge大法好 堆一个乌龟塔。每只乌龟有重量w和承重能力s(也要承受自己的重量,所以实际可托起s-w),问最多能堆几只乌龟 很明显是DP 从低往高堆不太好判断,因为不知道下面的乌龟还能不能承受得了。 切换到上帝视角,对于一个乌龟塔,我们可以直接把它托起来,然后在塔下面塞一只能承重的乌龟。 问...

dezhen7015的博客 201

UVa 10154 Weights and Measures DP

初始化DP数组各种WA,比人的初始memset(0x7f) 然后判断就直接

Commence,的艰苦历程 518

UVA - 10154 Weights and Measures

#include #include #include using namespace std; struct T { int w; int s; } A[5610]; int cmp (T a, T b) { return a.s < b.s; } int main() { int n, DP[5610], cur; memset(DP, 0x3f3f3f3f, sizeof

Bingo 1786

UVa 10154 - Weights and Measures

题目:有一些乌龟,他们分别有自己的重量和能负担的重量,求能叠在一起的乌龟的最大数量。 分析:dp,背包类似物。             状态:设f(i,j)为前i个物品叠成j高度时的最小总重量;             转移:f(i,j)= min(f(i-1,j),f(i-1,j-1)+w(i)) {取后面时,必须能够托起}。 说明:注意输入到EOF后处理。 #include #in

小白菜又菜 1809
上一篇: uva10534 - Wavio Sequence O(nlgn)的最长上升子序列
下一篇: uva10201 - Adventures in Moving - Part IV
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值