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 ( <= K <= ) different types of blocks with which to build the tower. Each block of type i has height h_i ( <= h_i <= ) and is available in quantity c_i ( <= c_i <= ). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i ( <= a_i <= ). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line : A single integer, K 

* Lines ..K+: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+ describes block type i.

Output

* Line : A single integer H, the maximum height of a tower that can be built

Sample Input


Sample Output


Hint

OUTPUT DETAILS: 

From the bottom:  blocks of type , below  of type , below  of type . Stacking  blocks of type  and  of type  is not legal, since the top of the last type  block would exceed height .

Source

 
这道题和 poj 1742 有点类似,只是这道题要先按每个block的最大高度进行排序,这样做的目的是为了获得最优解,想想怎么证明?
然后将dp数组初始化为-1,dp=-1表示取不到,dp[i]>=0表示取到i的时候还能剩下多少个
最后结果就是从最大高度开始寻找dp[i]!=-1的i的值,直接输出即可
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
#define N 406
#define M 40006
struct Node{
int h,a,c;
}block[N];
int dp[M*N];
bool cmp(Node a,Node b){
return a.a<b.a;
}
int main()
{
int n;
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
scanf("%d%d%d",&block[i].h,&block[i].a,&block[i].c);
}
sort(block,block+n,cmp); memset(dp,-,sizeof(dp));
dp[]=;
for(int i=;i<n;i++){
for(int j=;j<=M;j++){
if(dp[j]>=){
dp[j]=block[i].c;
}
else if(j<block[i].h || dp[j-block[i].h]<=){
dp[j]=-;
}
else if(j>block[i].a){
dp[j]=-;
}
else{
dp[j]=dp[j-block[i].h]-;
}
}
}
for(int i=M;i>=;i--){
if(dp[i]!=-){
printf("%d\n",i);
break;
}
} }
return ;
}

poj 2392 Space Elevator(多重背包+先排序)的更多相关文章

  1. POJ 2392 Space Elevator(多重背包变形)

    Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么? A: 是的, 需要根据 alt 对数组排序 Description The cows are going to space! T ...

  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. poj2392 Space Elevator(多重背包)

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

  6. poj[2392]space elevator

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

  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. hibernate 一对多双向关联 详解

    一.解析: 1.  一对多双向关联也就是说,在加载班级时,能够知道这个班级所有的学生. 同时,在加载学生时,也能够知道这个学生所在的班级. 2.我们知道,一对多关联映射和多对一关联映射是一样的,都是在 ...

  2. 使用awrextr.sql导出awr原始数据

    1.AWR原始数据与AWR报告的差别 AWR原始数据: 是oracle数据库mmon进程定期将统计量从内存转储至磁盘,并以结构化的形式存入若干张表组成自己主动工作负荷存储仓库(AutomaticWor ...

  3. POJ 1386 有向图欧拉通路

    题意:给你一些字符串,这些字符串可以首位相接(末位置如果和另一个字符串的首位置相同的话就可以相连) .然后问你是否可以全部连起来. 思路:就是取出每个字符串的首尾位置,然后求出出度和入度,根据有向欧拉 ...

  4. css中常用的标签

    最常用的标签 left 左 top 上 right 右 bottom 下 font 字体 size 大小 width 宽度 height 高度 class 类 label 标签 form 表单 gro ...

  5. HTML5 Canvas前台压缩图片并上传到服务器

    1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...

  6. C# Socket传输大文件

    1.基础类TransferFiles,client和server都需要 using System; using System.Collections.Generic; using System.Tex ...

  7. 启动外部exe程序

    Process myProcess = new Process();myProcess.StartInfo.FileName = pathName;myProcess.Start();其中的pathN ...

  8. 关于PagedDataSource,非常好用的一个分页属性!

    Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...

  9. Oracle—用户管理的备份(二)

    在用户管理的备份(一)中(详见:Oracle—用户管理的备份)对用户管理备份几种情况进行了说明:接下来说明几种特别情况和DBverify的使用. 一.如果在表空间在备份模式下,主机发生了异常关闭,会出 ...

  10. Go 解析JSON

    JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,具有自我描述性且易于让人阅读.尽管JSON是JavaScript的一个子集,但JSON是独立于 ...