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


[cpp] view plaincopy
  1. Weights and Measures  
  2. Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 144 Tried: 1427  
  3. SubmitStatusBest SolutionBack  
  4. Description  
  5. I know, up on top you are seeing great sights,   
  6. But down at the bottom, we, too, should have rights.   
  7. We turtles can't stand it. Our shells will all crack!   
  8. Besides, we need food. We are starving!" groaned Mack.   
  9.   
  10.   
  11. 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.   
  12. 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.  
  13.   
  14. Input  
  15. There is only one test case in the input.  
  16. 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.  
  17.   
  18. Output  
  19. Your output is a single integer indicating the maximum number of turtles that can be stacked without exceeding the strength of any one.  
  20.   
  21. Sample Input  
  22. 300 1000  
  23. 1000 1200  
  24. 200 600  
  25. 100 101  
  26.   
  27. Sample Output  
  28. 3  
  29.   
  30. Source  
  31. 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];

 

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <string.h>  
  4. #include <cmath>  
  5. #include <algorithm>  
  6. using namespace std;  
  7.   
  8. int dp[5650][5650];  
  9. typedef struct  
  10. {  
  11.     int w, s;  
  12. }tur;  
  13. tur t[5650];  
  14. int n;  
  15. bool comp( tur a, tur b)  
  16. {  
  17.     if( a.s <b.s||(a.s==b.s&& a.w< b.w))  
  18.         return true;  
  19.     return false;  
  20. }  
  21. int main()  
  22. {  
  23.     n=0;  
  24.     while( scanf("%d%d",&t[n].w,&t[n].s )!=EOF )n++;  
  25.     sort( t, t+n, comp);  
  26.     int i,j;  
  27.     memset( dp, 0x7f, sizeof( dp));  
  28.     for( i=0; i<=n; i++)  
  29.         dp[i][0] =0;  
  30.     for( i=1; i<=n; i++)  
  31.         for( j=1;j<=i; j++)  
  32.         {  
  33.             if(dp[i-1][j] <dp[i][j])  
  34.                 dp[i][j] =dp[i-1][j];  
  35.             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] )  
  36.                 dp[i][j] =dp[i-1][j-1]+t[i-1].w;  
  37.         }  
  38.     for( i=n; i>=0; i--)  
  39.         if( dp[n][i]<(1<<30))  
  40.             break;  
  41.     printf("%d\n",i);  
  42.     return 0;  
  43. }  


 

2.用滚动数组做,dp[j]表示选出j只乌龟的最小总重量。

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <algorithm>  
  3. #include <cstdio>  
  4. #include <cmath>  
  5. using namespace std;  
  6.   
  7. struct turtle  
  8. {  
  9.     int w,s;  
  10. }tur[5650];  
  11. int dp[5650];  
  12. bool comp(turtle a, turtle b)  
  13. {  
  14.     if( a.s< b.s||(a.s==b.s && a.w<b.w ))  
  15.             return true;  
  16.         return false;  
  17. }  
  18. int main()  
  19. {  
  20.     int n=1,i,j;  
  21.     while( cin>>tur[n].w >>tur[n].s ) n++;  
  22.     sort(tur+1, tur+n,comp);  
  23.     n--;  
  24.     for( i=1; i<=n; i++)  
  25.         dp[i] =1000000000;  
  26.     dp[0]=0;  
  27.     int ans=1;  
  28.     for( i=1; i<=n; i++)  
  29.         for( j=n; j>=1; j--)  
  30.         {  
  31.             if( dp[j-1]+tur[i].w<=tur[i].s)  
  32.                 dp[j] =min( dp[j], dp[j-1]+tur[i].w);  
  33.             if( dp[j]<1000000000 )  
  34.                 ans =max( j,ans);  
  35.         }  
  36.     cout <<ans<<endl;  
  37.     return 0;  
  38. }  
原创粉丝点击