POJ 2392 Space Elevator

Space Elevator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6236   Accepted: 2883

Description

The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000).

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

3
7 40 3
5 23 8
2 52 6

Sample Output

48

Hint

OUTPUT DETAILS:

From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.

Source

 
多重背包问题,先按最大高度排好序,再背包,注意判断是完全背包还是01背包。
 
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<iostream>
 5 using namespace std;
 6 int dp[40100] ;
 7 struct ddd{
 8     int h,a,c ;
 9 }f[410] ;
10 bool cmp(ddd a,ddd b){
11     return a.a < b.a ;
12 }
13 int main(){
14     int K;
15     while(scanf("%d",&K)!=EOF){
16         memset(dp,0,sizeof(dp)) ;
17         int ans = 0;
18         for(int i=1;i<=K;i++)
19             scanf("%d%d%d",&f[i].h,&f[i].a,&f[i].c) ;
20         sort(&f[1],&f[1]+K,cmp) ;
21         for(int x=1;x<=K;x++){
22             int a = f[x].a ,h = f[x].h ,c = f[x].c ;
23             if(h * c >= a){
24                 for(int i=h;i<=a;i++)
25                     if(dp[i-h]+h > dp[i])
26                         dp[i] = dp[i-h] + h ;
27             }    else{
28                 int temp = 1;
29                 while(temp < c){
30                     for(int i=a;i>=h*temp;i--)
31                         if(dp[i-temp*h] + temp * h > dp[i])
32                             dp[i] = dp[i-temp*h] + temp * h;
33                     c -= temp ;
34                     temp <<= 1;
35                 }
36                 for(int i=a;i>=h*c;i--)
37                     if(dp[i-c*h]+c*h>dp[i])
38                         dp[i] = dp[i-c*h] + c * h;
39             }    
40         }
41         for(int i=0;i<=f[K].a;i++)
42             if(ans < dp[i])
43                 ans = dp[i];
44         printf("%d\n",ans) ;
45     }
46     return 0;
47 }

 

posted @ 2012-08-16 23:10  3111006139  阅读(201)  评论(0编辑  收藏  举报