uestc oj 1510 Weights and Measures
来源:互联网 发布:集团合并报表软件 编辑:程序博客网 时间:2024/05/18 01:47
转自:http://blog.csdn.net/wangtaoking1/article/details/7338191
题目:http://acm.uestc.edu.cn/problem.php?pid=1510
- Weights and Measures
- Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 144 Tried: 1427
- SubmitStatusBest SolutionBack
- Description
- 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 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.
- 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.
- Input
- There is only one test case in the 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 (w<=s).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
- Source
- Liu Rujia
题意:有几只乌龟,每只乌龟有一定的重量与力量。每只乌龟可以背小于它力量的重量(包括它自身的重量)。问最多一共可以有多少只乌龟叠在一起。
可以证明,将力量大的乌龟放在下面可以得到一个更优的状态。因此在dp之前应先将所有乌龟按力量大小排好序,力量小的在前面。(证明可见这个网页)
1.用dp[i][j]表示从前 i 只乌龟中选出 j 只可以得打的最小总重量。
转移方程为:如果dp[i-1][j-1]+t[i].w <=t[i].s,dp[i][j] = min( dp[i-1][j-1]+t[i].w, dp[i-1][j] ); 如果不等,则dp[i][j] = dp[i-1][j];
- #include <iostream>
- #include <cstdio>
- #include <string.h>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int dp[5650][5650];
- typedef struct
- {
- int w, s;
- }tur;
- tur t[5650];
- int n;
- bool comp( tur a, tur b)
- {
- if( a.s <b.s||(a.s==b.s&& a.w< b.w))
- return true;
- return false;
- }
- int main()
- {
- n=0;
- while( scanf("%d%d",&t[n].w,&t[n].s )!=EOF )n++;
- sort( t, t+n, comp);
- int i,j;
- memset( dp, 0x7f, sizeof( dp));
- for( i=0; i<=n; i++)
- dp[i][0] =0;
- for( i=1; i<=n; i++)
- for( j=1;j<=i; j++)
- {
- if(dp[i-1][j] <dp[i][j])
- dp[i][j] =dp[i-1][j];
- if( dp[i-1][j-1]+t[i-1].w <=t[i-1].s &&dp[i-1][j-1]+t[i-1].w<dp[i][j] )
- dp[i][j] =dp[i-1][j-1]+t[i-1].w;
- }
- for( i=n; i>=0; i--)
- if( dp[n][i]<(1<<30))
- break;
- printf("%d\n",i);
- return 0;
- }
2.用滚动数组做,dp[j]表示选出j只乌龟的最小总重量。
- #include <iostream>
- #include <algorithm>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- struct turtle
- {
- int w,s;
- }tur[5650];
- int dp[5650];
- bool comp(turtle a, turtle b)
- {
- if( a.s< b.s||(a.s==b.s && a.w<b.w ))
- return true;
- return false;
- }
- int main()
- {
- int n=1,i,j;
- while( cin>>tur[n].w >>tur[n].s ) n++;
- sort(tur+1, tur+n,comp);
- n--;
- for( i=1; i<=n; i++)
- dp[i] =1000000000;
- dp[0]=0;
- int ans=1;
- for( i=1; i<=n; i++)
- for( j=n; j>=1; j--)
- {
- if( dp[j-1]+tur[i].w<=tur[i].s)
- dp[j] =min( dp[j], dp[j-1]+tur[i].w);
- if( dp[j]<1000000000 )
- ans =max( j,ans);
- }
- cout <<ans<<endl;
- return 0;
- }
- uestc oj 1510 Weights and Measures
- 10154 - Weights and Measures
- uva10154 - 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
- UVa 10154 - Weights and Measures
- 黑马程序员 JAVA基础<二> 面向对象之封装 继承 多态
- 杭电2081
- VS2012 单元测试步骤 c++
- 帧布局(FrameLayout)的简单使用
- 黑马程序员 JAVA基础<三> 多线程
- uestc oj 1510 Weights and Measures
- ubuntu 手动安装 Adobe Flash Player
- 再见,viewDidUnload方法
- 黑马程序员 JAVA基础<四> 集合
- android碎片Fragment简介详解
- 在vmware上安装redboot
- 黑马程序员 JAVA基础<五> IO流
- uestc oj 1690 最少花费
- 为何 IntelliJ IDEA 比 Eclipse 更好