POJ 2392 Space Elevator

Space Elevator

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 2392
64-bit integer IO format: %lld      Java class name: Main
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

 
解题:多重背包吧。。。记得要排序,把最高高度低的放前面,因为低的放前面可以更好的更新后面的。。。
 
POJ 2392 Space Elevator
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <algorithm>

 5 using namespace std;

 6 const int maxn = 400010;

 7 struct node{

 8     int h,c,a;

 9     bool operator<(const node &t) const{

10         return a < t.a;

11     }

12 }b[401];

13 int dp[maxn],cnt[maxn];

14 int main(){

15     int n;

16     while(~scanf("%d",&n)){

17         for(int i = 0; i < n; ++i)

18             scanf("%d %d %d",&b[i].h,&b[i].a,&b[i].c);

19         sort(b,b+n);

20         memset(dp,0,sizeof dp);

21         int ans = 0;

22         for(int i = 0; i < n; ++i){

23             memset(cnt,0,sizeof cnt);

24             for(int j = b[i].h; j <= b[i].a; ++j){

25                 if(dp[j] < dp[j-b[i].h] + b[i].h && cnt[j-b[i].h] < b[i].c){

26                     dp[j] = dp[j-b[i].h] + b[i].h;

27                     cnt[j] = cnt[j-b[i].h] + 1;

28                     ans = max(ans,dp[j]);

29                 }

30             }

31         }

32         printf("%d",ans);

33     }

34     return 0;

35 }
View Code

 

你可能感兴趣的:(poj)