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. Oracle Berkeley DB Java 版

    Oracle Berkeley DB Java 版是一个开源的.可嵌入的事务存储引擎,是完全用 Java 编写的.它充分利用 Java 环境来简化开发和部署.Oracle Berkeley DB Ja ...

  2. 最好的Laravel中文文档

    分页 配置 基本用法 给分页链接添加自定义信息 配置 在其它的框架中,分页有时很痛苦. 但是Laravel让分页简单到不可思议. 默认Laravel包含了两个分页视图, 在app/config/vie ...

  3. 小学生之KTV播放原理

    第一步: 创建一个Song类 //歌曲名称 public  string SongName { get; set; } //歌曲路劲 public string SongPath { get; set ...

  4. JQuery中常用的 属性选择器

    jQuery中使用$()作为选择符极大提高工作效率以及方便快捷;一些常用属性的选择器由以下几种 1) $('#id') id选择器 2) $('.class') css选择器,class类名 3) $ ...

  5. XML中 添加或修改时 xmlns="" 怎么删除

    //创建节点时 记得加上  ---> xmldoc.DocumentElement.NamespaceURI XmlElement url = xmldoc.CreateElement(&quo ...

  6. excel运行最多行数

    2003及以前版本为65536(即6万多)行,256列2007版:1048576(即1百零多万)行,16384(即1千多)列

  7. 找两个string[ ]里不同的元素

    方法 1:string[] strListOne = new string[] {"Type", "Parent Id","Status"} ...

  8. Swift 面向对象

    import Foundation class Hi{ func sayHi(){ print("HI jinpanpang") } } class Hello:Hi { var ...

  9. 创建自托管的SignalR服务端

    微软官方例子地址:http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host 1.说明: SignalR服务端可 ...

  10. 12 hdfs常用文件、目录拷贝操作、删除操作

    package com.da.hbase.tool.utils; import com.da.hbase.tool.common.Const; import org.apache.hadoop.con ...