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_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.

题目大意:
有k种砖,每种砖给出单块高度h_i,且使用第i种砖后高度不能超过a_i,每种砖数量为c_i,要求用这些砖能够组成的最大高度

解题分析:
该题目属于多重部分和问题的变种,多出了一个使用砖块之后高度不能超出a_i的条件,所以需要适当修改一下代码中的判断条件。
根据贪心思想,a_i越小的砖块应该堆在越下面,所以先对a_i升序排序。这样的话,当检查到第n种石头时,可以组成的最大高度为前n种里的最大值a_n。接下来便是多重部分和的模板问题了。

多重部分和问题的模板见我之前的一篇博客
POJ 1742 Coins 动态规划 多重部分和问题 解题模板

AC代码:

//考虑多重部分和问题
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
struct node {
	int len, level, num;
	bool operator < (const node &a) const {
		return level < a.level;//按照level从小到大排序
	}
}list[401];
int dp[40001];//存放使用的砖块数
bool can_buy[40001];
int main() {
	int k;
	scanf("%d", &k);
	for (int i = 1; i <= k; i++) {
		scanf("%d%d%d", &list[i].len, &list[i].level, &list[i].num);
	}
	sort(list + 1, list + 1 + k);
	for (int m = 1; m <= list[k].level; m++)can_buy[m] = false;
	can_buy[0] = true;
	for (int i = 1; i <= k; i++) {
		for (int m = 0; m <= list[k].level; m++)dp[m] = 0;
		for (int j = list[i].len; j <= list[i].level; j++) {
			if (!can_buy[j] && can_buy[j - list[i].len] && dp[j - list[i].len] < list[i].num) {
				can_buy[j] = true;
				dp[j] = dp[j - list[i].len] + 1;
			}
		}
	}
	for (int i = list[k].level; i >= 0; i--) {
		if (can_buy[i]) {
			cout << i << endl;
			break;
		}
	}
	//system("pause");
}
上一篇:文件系统


下一篇:cf 1141/problem/F2 Same Sum Blocks (Hard)