当前位置: 代码迷 >> 综合 >> POJ 2392——Space Elevator【多重背包 二进制优化 贪心】
  详细解决方案

POJ 2392——Space Elevator【多重背包 二进制优化 贪心】

热度:21   发布时间:2023-12-16 23:08:57.0

题目传送门


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.


题意:

n件物品,第i件hi高,有ci件,最高的一件不能超过ai的高度。问最高能堆多高


分析:

  • 多重背包 & 二进制优化
  • 拆分时注意拆分上线并非为c,而应该是min(c, a/h)
  • dp时注意添加判断条件
  • 每次背包选取物品时都应该优先放置当前ai值最小的(贪心)


AC代码:

#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <fstream>
#include <set>
using namespace std;
typedef long long ll;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);#define INF 0x3f3f3fconst int MAXN = 2e6 + 7;int dp[MAXN];
struct node {
    int v, w;bool operator < (const node& t)const {
    return w < t.w;}
}node[MAXN];
int main() {
    ios;int n;while (cin >> n) {
    int sum = 0;memset(dp, 0, sizeof(dp));int k = 0;for (int i = 1; i <= n; i++) {
    int v, w, c;cin >> v >> w >> c;sum += c * v;c = min(c, w / v);for (int j = 1; j <= c; j *= 2) {
    node[k].v = v * j;node[k++].w = w;c -= j;}if (c > 0) {
    node[k].v = v * c;node[k++].w = w;}}sort(node, node + k);for (int i = 0; i < k; i++) {
    for (int j = sum; j >= node[i].v; j--)if (j <= node[i].w)dp[j] = max(dp[j], dp[j - node[i].v] + node[i].v);}int ans = 0;for (int i = sum; i >= 0; i--)ans = max(ans, dp[i]);cout << ans << endl;}return 0;
}
  相关解决方案