Space Elevator
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 0 Accepted Submission(s) : 0
Problem 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.
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
题意:一群牛要上太空,给出n种石块,每种石块给出单块高度,总高度不能超过的最大值,数量,要求用这些石块能组成的最大高度
先把将允许最大高度的值 从小到达排序。。然后多重背包。。。value 及 高度 要开个数组记录 放置了多少个。 因为这个背包的花费跟价值是一样的,而且为了贪心求得最大值,所以。。。不太好描述,见代码。。
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <utility>
#include <queue>
using namespace std;
const int maxn = 400000;
const int INF = 0x3fffffff;
struct block {
int h, a, c;
}a[405];
int dp[400005];
int num[400005];
int s[105], f[105];
bool cmp(const block& b1, const block b2) {
return b1.a < b2.a;
}
int main() {
//freopen("in.txt", "r", stdin);
int n;
while (cin >> n) {
int altitude = 0;
for (int i = 0; i < maxn; ++i)
dp[i] = -INF;
dp[0] = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i].h >> a[i].a >> a[i].c;
altitude = max(a[i].a, altitude);
}
sort(a, a + n, cmp);
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= altitude; ++j) {
num[j] = 0;
}
for (int j = a[i].h; j <= a[i].a; ++j) {
if (dp[j - a[i].h] > -INF && dp[j] == -INF && num[j - a[i].h] < a[i].c) {
dp[j] = j;
num[j] = num[j - a[i].h] + 1;
//cout << num[j] << endl;
}
}
}
int ans = 0;
for (int i = altitude; i >= 0; --i) {
//cout << dp[i] << " " << i << endl;
if (dp[i] > -INF) {
ans = i;
break;
}
}
cout << ans << endl;
}
}