uva10154 - Weights and Measures
来源:互联网 发布:武汉网络推广招聘 编辑:程序博客网 时间:2024/05/18 03:30
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 10001000 1200200 600100 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 0x3f3f3f3fusing 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;}
- uva10154 - Weights and Measures
- uva10154 - Weights and Measures(01背包)
- UVA10154:Weights and Measures(类01背包)
- 10154 - Weights and Measures
- uva 10154 Weights and Measures
- UVa 10154. Weights and Measures
- UVA 10154 Weights and Measures
- UVa 10154 - Weights and Measures
- uva 10154 - Weights and Measures
- UVa:10154 Weights and Measures
- UVA - 10154 Weights and Measures
- UVA 10154 Weights and Measures
- UVA - 10154 Weights and Measures
- UVA - 10154 Weights and Measures
- UVa 10154 - Weights and Measures
- UVA - 10154 Weights and Measures
- UVA 10154 - Weights and Measures
- uva 10154 Weights and Measures
- Foreign Language_english_语法分析_1
- 来美国大半年了,又开始想家了。
- jAVA反射机制
- 第七周上机实践项目——给定两个正整数,求出两数的正差值并输出
- Linq使用总结
- uva10154 - Weights and Measures
- 三阶魔方入门手法
- (android实战)破解apk(转载)
- 九牛之人
- 用Hibernate将Date类型的数据按格式存入MySQL
- 马云说了:年轻人,你不去创业,不去 旅游
- NSValue 札记
- MDK5.00免刷Jlink升级方法
- 基础概念:jdk与jre的区别