Space Elevator
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12817 | Accepted: 6092 |
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.
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.
* 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.
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
题意:
用物块堆塔,每一个物块都有不同的高度以及数量,并且每一种物块能够堆放的最大高度也是有限制的。求利用这些物块能堆的最高高度。
思路:完全背包,可以利用单调队列.因为不同类型的物块能堆放的最大高度不同,最大高度限制越低的物块应当越早堆放越好,故一开始要以最大高度限制对不同种类的物块从小到大排序。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<cmath> #include<cstring> using namespace std; #define N_MAX 400000+20 struct tower { int w, v, m, a;//m是数量,a是当前积木能堆积的最高高度 tower() {} bool operator < (const tower &b) const{ return a < b.a; } }T[400+20]; int n; int dp[N_MAX]; int deq[N_MAX],deqv[N_MAX]; void solve() { //memset(dp, 0, sizeof(dp)); memset(deq, 0, sizeof(deq)); memset(deqv,0,sizeof(deqv)); for (int i = 0; i < n;i++) { for (int a = 0; a < T[i].w;a++) { int s = 0, t = 0; for (int j = 0; j*T[i].w + a <= T[i].a; j++) { int val = dp[j*T[i].w + a] - j*T[i].v; while (s < t&&deqv[t - 1] <= val)t--; deq[t] = j; deqv[t++] = val; dp[j*T[i].w + a] = deqv[s] + j*T[i].v; if (deq[s] == j - T[i].m) s++; } } } } int main() { scanf("%d",&n); for (int i = 0; i < n;i++) { scanf("%d%d%d",&T[i].w,&T[i].a,&T[i].m); T[i].v =T[i].w; } sort(T,T+n); solve(); int max_height = *max_element(dp,dp+N_MAX-19);; printf("%d ",max_height); return 0; }