DP:Space Elevator(POJ 2392)

             

                  太空电梯

  题目大意:一群牛想造电梯到太空,电梯都是由一个一个块组成的,每一种块不能超过这个类型的高度,且每一种块都有各自的高度,有固定数量,问最高能造多高。

  这题就是1742的翻版,对ai排个序就可以了

  (尼玛,我qsort排了n-1个数,wa半天不知所措)

 1 #include <iostream>
 2 #include <functional>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 typedef struct _set
 7 {
 8     int h_i;
 9     int max_h;
10     int count;
11 }Block;
12 int fcomp(const void *a, const void *b)
13 {
14     return (*(Block *)a).max_h - (*(Block *)b).max_h;
15 }
16 
17 static int dp[40005];
18 static Block B_Set[404];
19 
20 void Search(const int);
21 
22 int main(void)
23 {
24     int n;
25     while (~scanf("%d", &n))
26     {
27         if (n == 0) continue;
28         for (int i = 1; i <= n; i++)
29             scanf("%d%d%d", &B_Set[i].h_i, &B_Set[i].max_h, &B_Set[i].count);
30         qsort(B_Set, n + 1, sizeof(Block), fcomp);
31         Search(n);
32     }
33     return 0;
34 }
35 
36 void Search(const int n)
37 {
38     int i, j;
39     memset(dp, -1, sizeof(dp));
40     dp[0] = 0;
41     for (i = 1; i <= n; i++)
42     {
43         if (B_Set[i].h_i == 0) continue;
44         for (j = 0; j < B_Set[i].h_i && j <= B_Set[i].max_h; j++)//先把前面的几个包确定下来
45             if (dp[j] != -1)
46                 dp[j] = B_Set[i].count;
47         for (; j <= B_Set[i].max_h; j++)
48         {
49             if (dp[j] == -1)
50             {
51                 if (dp[j - B_Set[i].h_i] <= 0)
52                     continue;
53                 else dp[j] = dp[j - B_Set[i].h_i] - 1;
54             }
55             else dp[j] = B_Set[i].count;
56         }
57     }
58     for (int i = B_Set[n].max_h; i >= 0; i--)
59     {
60         if (dp[i] >-1)
61         {
62             printf("%d\n", i);
63             break;
64         }
65     }
66 }

posted @ 2015-11-05 01:31  PhiliAI  阅读(243)  评论(1编辑  收藏  举报