Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么?

A: 是的, 需要根据 alt 对数组排序

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. 对数据预处理, 按照 alt 排序
  2. 预处理, 倍增优化, 转化成 01 背包
  3. 求解 01 背包

总结:

1. 对 3 个数组, 以其中一个为根据排序, 一种解决方法是把三个数组绑定到一个对象中, 然后对对象排序

2. 犯了个致命的错误, 48行代码写成memset(dp, 0, sizeof(int)*V); 导致当 V 比较大时, h 也被置 0 了

代码:

#include <iostream>
#include <algorithm>
using namespace std;
int K;
const int MAXN = 40010;
bool dp[40010];
int h[MAXN], a[MAXN];
int V; struct block {
int h, a, c;
bool operator<(const block &other) const {
return this->a < other.a;
}
}; block blocks[MAXN];
int pre_process() {
sort(blocks, blocks+K);
int len = 0;
for(int i = 0; i < K; i ++) {
int rem = blocks[i].c;
int j = 0;
// 预处理,
while(rem) {
if(rem >= (1<<j)) {
h[len] = (1<<j)*blocks[i].h;
a[len] = blocks[i].a;
rem -= (1<<j);
j++;
if(h[len] > a[len])
continue;
len++;
}else{
h[len] = rem*blocks[i].h;
a[len] = blocks[i].a;
len++;
rem = 0;
}
}
}
return len;
} int solve_dp() {
int len = pre_process();
// 01背包
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for(int i = 0; i < len; i ++) {
for(int v = V; v >= h[i]; v--) {
if(v > a[i]) continue;
if(!dp[v] && dp[v-h[i]]) {
dp[v] = true;
//printf("dp[%d] = true\n", v);
}
}
} for(int v = V; v >= 0; v --)
if(dp[v])
return v;
}
int main() {
freopen("E:\\Copy\\ACM\\测试用例\\in.txt", "r", stdin);
cin >> K;
int h_i, a_i, c_i;
V = 0;
for(int i = 0; i < K; i ++) {
scanf("%d%d%d", &blocks[i].h, &blocks[i].a, &blocks[i].c);
V = min(V+blocks[i].h*blocks[i].c, 40000);
} cout << solve_dp() << endl;
return 0;
}

  

update: 2014年3月14日10:35:10

1. 贪心策略是最大高度较小的先放

Q1: 对物品排序会对结果产生影响吗

A1: 会的. 假设只有两个物品, 最大高度不同, 显然最大高度较小的放前面可以获摆放的更高

Q2: 第二层循环, V 是怎么初始化的

A2: 上面的代码初始化比较随意, V 初始化为 40000 和 所有梯子高度之和 的较小值

POJ 2392 Space Elevator(多重背包变形)的更多相关文章

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

    Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...

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

    POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...

  3. POJ 2392 Space Elevator(多重背包)

    显然塔的总高度不会超过最大的a[i],而a[i]之前的可以到达的高度 是由a值更小的块组成,所以按照a从小到大的顺序去转移. 然后就是多重背包判断存在性了,几乎和coin那题一样. 数据没coin丧病 ...

  4. POJ 2392 Space Elevator 背包题解

    多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...

  5. poj[2392]space elevator

    Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...

  6. poj2392 Space Elevator(多重背包)

    http://poj.org/problem?id=2392 题意: 有一群牛要上太空.他们计划建一个太空梯-----用一些石头垒.他们有K种不同类型的石头,每一种石头的高度为h_i,数量为c_i,并 ...

  7. POJ 2392 Space Elevator DP

    该题与POJ 1742的思路基本一致:http://www.cnblogs.com/sevenun/p/5442279.html(多重背包) 题意:给你n个电梯,第i个电梯高h[i],数量有c[i]个 ...

  8. POJ 2392 Space Elevator 贪心+dp

    题目链接: http://poj.org/problem?id=2392 题意: 给你k类方块,每类方块ci个,每类方块的高度为hi,现在要报所有的方块叠在一起,每类方块的任何一个部分都不能出现在ai ...

  9. poj 1742 Coins (多重背包)

    http://poj.org/problem?id=1742 n个硬币,面值分别是A1...An,对应的数量分别是C1....Cn.用这些硬币组合起来能得到多少种面值不超过m的方案. 多重背包,不过这 ...

随机推荐

  1. mvn项目中的pom文件提示Error parsing lifecycle processing instructions解决

    清空.m2/repository下的所有依赖文件,重新下载即可解决该问题. 如果本地用户下没有.m2/repository 目录,找到如下mvn 指定的repository,进去之后清空所有文件.

  2. Winform快速导出

    public static void ExportExcel(DataGridView DataGridView01){ Stream stream = null; StreamWriter writ ...

  3. PHP简单工厂模式、工厂方法模式和抽象工厂模式

    PHP工厂模式概念:工厂模式是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动 ...

  4. 【C#】复制物体后,给副本命名的规则——用正则表达式

    需求:已经实现物体复制功能后,给新生成的副本进行命名. 问题:因为副本也能被复制,即副本的副本,所以复制品的命名规则会如下图所示. 尝试过用for循环和递归,发现在for循环内部递归返回的话会有问题, ...

  5. stm8s + si4463 寄存器配置

    /***********************************************函 数: main功 能: 程序入口输 入: /输 出: /描 述: /**************** ...

  6. dac7562 应用层实现dac

    /* * dac7562 (using spidev driver) *  */ #include <stdint.h>#include <unistd.h>#include ...

  7. java资料——线性表(转)

    线性表 线性表(亦作顺序表)是最基本.最简单.也是最常用的一种数据结构.线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的.线性表的逻辑结构简单, ...

  8. unarchive模块

    unarchive模块 用于解压文件,模块包含如下选项: copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes.若为no,则要求目标主机上压缩包必须存在. creates:指定一个文件名 ...

  9. 轻量级ORM框架Dapper应用三:使用Dapper实现In操作

    IN 操作符允许我们在 WHERE 子句中规定多个值. 本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下: using System; using Syste ...

  10. dubbo注册中心zookeeper出现异常 Opening socket connection to server 10.70.42.99/10.70.42.99:2181. Will not attempt to authenticate using SASL (无法定位登录配置)

    linux下,zookeeper安装并启动起来了 DEMO时,JAVA控制台出现: INFO 2014-03-06 09:48:41,276 (ClientCnxn.java:966) - Openi ...