Space Elevator
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8569   Accepted: 4052

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种石块,每种石块给出单块高度,每种石块都有个限定高度,超过这个高度这种石块就不能被使用了,最后面是每种石块的数量,要求用这些石块能组成的最大高度,并且不能超过限定的高度;

思路:在进行多重背包之前要进行一次排序,将最大高度小的放在前面,只有这样才能得到最优解,如果将大的放在前面,后面有的小的就不能取到,排序之后就可以进行完全背包了
 AC代码(一): PZ
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define max 40005
int a[max],b[max],c[max];
struct pp
{
int x,y,z;
}p[];
int cmp(pp p1,pp p2)//按照限定高度进行排序
{
return p1.y<p2.y;
}
int main()
{
int n;
while((scanf("%d",&n))!=EOF)
{
int i,j,k,t;
memset(a,,sizeof(a));
for(i=;i<n;i++)
scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].z);
sort(p,p+n,cmp);
j=;
//暴力枚举所有的高度,a[s]=1; 表示s这个高度能够达到;
for(i=;i<n;i++)
{
int s=,t=,dd=;
while(s<=p[i].y&&t<=p[i].z) //小于限定的高度,并且小于限定的个数
{
s=s+p[i].x;
for(k=;k<j;k++)
if(!a[b[k]+s] && (b[k]+s<=p[i].y)) //如果b[k]+s 这个高度没有出现过,并且小于限定的高低,高度可行
{
a[b[k]+s]=;
c[dd++]=b[k]+s;
}
if(!a[s] && s<=p[i].y)
{
a[s]=;
b[j++]=s;
}
t++; //统计当前石块使用个数
}
for(k=;k<dd;k++)
b[j++]=c[k];
}
int ok=;
for(i=;i>=;i--)
if(a[i])
{
printf("%d\n",i);
ok=;
break;
}
if(ok)
printf("0\n");
}
return ;
}

AC代码(二):

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = ;
struct TT
{
int x,h,n;
}a[];
int dp[N],cot[N];
int cmp(TT x,TT y)//按照最高能堆多高进行排序;
{
if( x.h<y.h) return ;
return ;
}
int main()
{
int T,ans;
while(cin>>T)
{
for(int i=; i<T; i++)
scanf("%d %d %d",&a[i].x,&a[i].h,&a[i].n);
memset(dp,,sizeof());
sort(a,a+T,cmp);
dp[] = ;
ans = ;
//采用暴力的方法,一直枚举j,看j最大能达到多少,则j就是要找的最大值;
for(int i=; i<T; i++)
{
memset(cot,,sizeof(cot));
for(int j = a[i].x; j<=a[i].h; j++)
{
// 如果j还没有达到并且dp[j-a[i].x] 大于0,并且当前模板的使用数不大于它的总数
if(!dp[j] && dp[j-a[i].x] && cot[j-a[i].x] < a[i].n)
{
dp[j] = ;
cot[j]=cot[j-a[i].x]+;
if(j>ans) ans = j;
}
}
}
printf("%d\n",ans);
}
return ;
}

poj2392 Space Elevator(多重背包问题)的更多相关文章

  1. poj2392 Space Elevator(多重背包)

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

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

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

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

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

  4. POJ2392 Space Elevator

    题目:http://poj.org/problem?id=2392 一定要先按高度限制由小到大排序! 不然就相当于指定了一个累加的顺序,在顺序中是不能做到“只放后面的不放前面的”这一点的! 数组是四十 ...

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

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

  6. POJ2392:Space Elevator

    Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9244   Accepted: 4388 De ...

  7. A - Space Elevator(动态规划专项)

    A - Space Elevator Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  8. poj[2392]space elevator

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

  9. 多重背包问题:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(HDU 2191)(二进制优化)

    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 HDU 2191 一道裸的多重背包问题: #include<iostream> #include<algorithm> #i ...

随机推荐

  1. Lua-简洁、轻量、可扩展的脚本语言

    转自:http://rmingwang.com/The-Programming-Language-Lua.html 1. 一.Lua安装及常用库的安装 2. 1.1 Ubuntu $ sudo apt ...

  2. xss编码小结

    一.JS编码与HTML编码区分: HTML实体可以使用十进制与十六进制编码:javascript可以使用Unicode与八进制与十六进制进行编码. 二.编码原理区分: 三.编码与非编码 对于JS编码: ...

  3. time_t和SYSTEMTIME之间的相互转换 【转】

    time_t和SYSTEMTIME之间的相互转换 #include <ctime> /* **time_t转SYSTEMTIME */ SYSTEMTIME TimetToSystemTi ...

  4. OpenGL ES入门09-GLSL实现常见特效 [转]

    本文转自简书,原文地址http://www.jianshu.com/p/e4a8c83cd37 本文是关于OpenGL ES的系统性学习过程,记录了自己在学习OpenGL ES时的收获. 这篇文章的目 ...

  5. Github开源项目分享

    Type LibDescription   LibLocation Android SlidingMenu   https://github.com/jfeinstein10/SlidingMenu ...

  6. (转)Dubbo扩展点实现细节

    实现细节 (+) (#) 初始化过程细节 (+) (#) 解析服务 基于dubbo.jar内的META-INF/spring.handlers配置,Spring在遇到dubbo名称空间时,会回调Dub ...

  7. 阅读源代码的重要性:如厨师选食材,耍厨具——在Eclipse中怎样查看Java、Android源代码

    首先,非常多人说,不会看jdk中的源代码就不叫学过Java.显然这是肯定的.打个例如:真正的厨师须要从食材的选取.加工.到最后的烹饪.装盘成型,甚至到最后给用户介绍食用方法等一整套流程走下来.而实际上 ...

  8. Codeforces 112A-Petya and Strings(实现)

    A. Petya and Strings time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. cocos2d-x 托付模式的巧妙运用——附源代码(二)

    转载请注明出处:http://blog.csdn.net/hust_superman/article/details/38292265,谢谢. 继上一篇将了托付类的详细实现后.这篇来将一下怎样在游戏中 ...

  10. Redis Cluster集群的搭建

    redis集群搭建原理: redis是单线程,但是一般的作为缓存使用的话,redis足够了,因为它的读写速度太快了.   官方的一个简单测试: 测试完成了50个并发执行100000个请求. 设置和获取 ...