码迷,mamicode.com
首页 > 其他好文 > 详细

动态规划_多重背包:Space Elevator

时间:2015-08-07 13:06:14      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

U - Space Elevator
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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.

 

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int MAX = 40010;
 6 const int INF = 0x3f3f3f3f;
 7 
 8 struct Node
 9 {
10     int h;
11     int a;
12     int c;
13 };
14 
15 int ans;
16 bool vis[MAX];
17 Node block[MAX];
18 
19 bool cmp(Node a, Node b)
20 {
21     return (a.a < b.a);
22 }
23 
24 void zero_pack(int w,int v)
25 {
26     for(int i = v; i >= w; i--)
27         if(vis[i-w])
28         {
29             vis[i] = true;
30             ans = max(ans,i);
31         }
32 }
33 
34 void complete_pack(int w, int v)
35 {
36     for(int i = w; i <= v; i++)
37         if(vis[i - w])
38         {
39             vis[i] = true;
40             ans = max(ans,i);
41         }
42 }
43 
44 void multi_pack(int w, int n, int v)
45 {
46     if(w * n > v)
47         complete_pack(w,v);
48     else
49     {
50         for(int i = 1; i < n; i *= 2)
51         {
52             n -= i;
53             zero_pack(w * i, v);
54         }
55 
56         zero_pack(n * w,v);
57     }
58 }
59 
60 int main()
61 {
62 #ifdef OFFLINE
63     freopen("in.txt", "r", stdin);
64     freopen("out.txt", "w", stdout);
65 #endif
66 
67     ans = 0;
68     memset(vis,false,sizeof(vis));
69 
70     int k;
71     scanf("%d", &k);
72     for(int i = 0; i < k; i++)
73         scanf("%d%d%d", &block[i].h, &block[i].a, &block[i].c);
74     
75     vis[0] = true;
76     sort(block, block + k, cmp);
77     
78     for(int i = 0; i < k; i++)
79         multi_pack(block[i].h, block[i].c, block[i].a);
80     
81     printf("%d\n", ans);
82     return 0;
83 
84 }
View Code

 

动态规划_多重背包:Space Elevator

标签:

原文地址:http://www.cnblogs.com/gwsbhqt/p/4710156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!