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

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 }

时间: 2024-10-30 04:00:05

动态规划_多重背包:Space Elevator的相关文章

【动态规划】多重背包

贵有恒,何必三更起五更眠:最无益,莫过一日曝十日寒. [动态规划]多重背包 时间限制: 1 Sec  内存限制: 64 MB提交: 5  解决: 5[提交][状态][讨论版] 题目描述 张琪曼:“魔法石矿里每种魔法石的数量看起来是足够多,但其实每种魔法石的数量是有限的.” 李旭琳:“所以我们需要改变装包策略啦.” 现有N(N≤10)种魔法石和一个容量为V(0<V<200)的背包.第i种魔法石最多有n[i]件可用,每个占用的空间是c[i],价值是w[i].全部物品总数不超过50.求解将哪些魔法石

悼念512汶川大地震遇难同胞――珍惜现在,感恩生活(背包九讲_多重背包)

悼念512汶川大地震遇难同胞――珍惜现在,感恩生活Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价

Big Event in HDU(背包九讲_多重背包转01背包)

Big Event in HDUCrawling in process... Crawling failed Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you

poj 2392 Space Elevator (多重背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8110   Accepted: 3843 题目大意  :一群牛要上天  用一些石块堆塔  给出石块的种类  及其每个种类的数量 和该种石块能出现的最高高度  和每种石块的数量 求怎么摆放才能堆得最高 多重背包模板题.... 将所有石块排序  把高度低的放下面 #include<iostream> #include<cstdio>

POJ 2392 Space Elevator(多重背包)

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

POJ 2392 Space Elevator(贪心+多重背包)

POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木,每种积木都有一个高度h[i],一个数量num[i],还有一个限制条件,这个积木所在的位置不能高于limit[i],问能叠起的最大高度? 分析: 本题是一道多重背包问题, 不过每个物品的选择不仅仅要受该种物品的数量num[i]限制, 且该物品还受到limit[i]的限制. 这里有一个贪心的结论: 我们每次背包选取物品时都应该优先放置当前limi

poj 2392 Space Elevator(多重背包+先排序)

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

POJ 题目2392 Space Elevator(多重背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9042   Accepted: 4296 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 <

POJ2392Space Elevator(多重背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8701   Accepted: 4135 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 <