poj2392 Space Elevator( dp ,背包)

奶牛们上天了。。 有n种材料,每种材料高hi , 限制最大放置高度ai , 有ci块, 问能搭多高

首先对每种材料按照ai排序, 然后从小到大, dp[i][j] 表示前i中材料在j的高度下,第i种能剩多少,最后只要从amax往下判断第一个>=0的就可以了

题目:

Space Elevator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7638   Accepted: 3606

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

 
 
代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 //#pragma comment (linker,"/STACK:1024000000,1024000000")
 6 #define MAXN 500
 7 #define INF 0x7ffffff
 8 int n;
 9 struct P
10 {
11     int h,c,a;
12 
13     bool operator < (const P& x) const
14     {
15         return a<x.a;
16     }
17 };
18 
19 int dp[MAXN][40000+10];
20 
21 P w[MAXN];
22 int hmax = 0;
23 void init()
24 {
25     cin>>n;
26     for(int i=0;i<n;i++)
27     {
28         cin>>w[i].h>>w[i].a>>w[i].c;
29         hmax  = max(hmax , w[i].a);
30     }
31     sort(w,w+n);
32     for(int i=0;i<=n;i++)
33     {
34         for(int ta=0;ta<=hmax;ta++)
35             dp[i][ta] = -1;
36     }
37     dp[0][0] = 0;
38 }
39 
40 int main()
41 {
42     init();
43 
44     for(int i=1;i<=n;i++)
45     {
46         for(int ta = 0 ; ta<=w[i-1].a;ta++)
47         {
48             if( dp[i-1][ta] >=0 )
49             {
50                 dp[i][ta]  = w[i-1].c;
51             }
52             else if( ta<w[i-1].h|| dp[i][ ta - w[i-1].h]<=0)
53             {
54                 dp[i][ta] = -1;
55             }
56             else
57             {
58                 dp[i][ta] = dp[i][ ta- w[i-1].h] - 1;
59             }
60         }
61     }
62 
63     for(int i=hmax;i>=0;i--)
64     {
65         if(dp[n][i]>=0)
66         {
67             cout<<i<<endl;
68             break;
69         }
70     }
71 
72     return 0;
73 }
原文地址:https://www.cnblogs.com/doubleshik/p/3538415.html