poj2392 Space Elevator(多重背包问题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8569 | Accepted: 4052 |
Description
Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.
Input
* 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
Sample Input
3
7 40 3
5 23 8
2 52 6
Sample Output
48
Hint
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.
#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(多重背包问题)的更多相关文章
- poj2392 Space Elevator(多重背包)
http://poj.org/problem?id=2392 题意: 有一群牛要上太空.他们计划建一个太空梯-----用一些石头垒.他们有K种不同类型的石头,每一种石头的高度为h_i,数量为c_i,并 ...
- poj 2392 Space Elevator(多重背包+先排序)
Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...
- POJ 2392 Space Elevator(多重背包变形)
Q: 额外添加了最大高度限制, 需要根据 alt 对数据进行预处理么? A: 是的, 需要根据 alt 对数组排序 Description The cows are going to space! T ...
- POJ2392 Space Elevator
题目:http://poj.org/problem?id=2392 一定要先按高度限制由小到大排序! 不然就相当于指定了一个累加的顺序,在顺序中是不能做到“只放后面的不放前面的”这一点的! 数组是四十 ...
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- POJ2392:Space Elevator
Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9244 Accepted: 4388 De ...
- A - Space Elevator(动态规划专项)
A - Space Elevator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj[2392]space elevator
Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...
- 多重背包问题:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(HDU 2191)(二进制优化)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 HDU 2191 一道裸的多重背包问题: #include<iostream> #include<algorithm> #i ...
随机推荐
- Python 学习建议(个人愚见)
前言 本科毕业已经5年+,一直在做iOS开发. 工作方面:从刚入门的小菜鸟码农,后面到BAT里混过两年,到现在带10个人的Team Leader,收入尚可. 生活状态:已婚,儿子刚满1岁,有一定存款, ...
- js中几种常见的方法的实例 shift,unshift,push,prop
1.shift()定义和用法 shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值. 语法:arrayObject.shift() 返回值:数组原来的第一个元素的值. 说明:如果 ...
- jQuery中,选择器既匹配开头又匹配结尾
jQuery中,选择器既匹配开头又匹配结尾的方法: [attr^=val]attr$=val [attr^=val][attr$=val]
- [转]Configure logging in SSIS packages
本文转自:http://learnsqlwithbru.com/2009/11/26/configure-logging-in-ssis-packages/ n this article we wil ...
- python学习:Windows 下 Python easy_install 的安装
Windows 下 Python easy_install 的安装 下载安装python安装工具下载地址:http://pypi.python.org/pypi/setuptools 可以找到 ...
- 流畅的python第八章对象引用,可变性和垃圾回收
变量不是盒子 在==和is之间选择 ==比较两个对象的值,而is比较对象的标识 元组的相对不可变姓 元组与多数的python集合(列表,字典,集,等等)一样,保存的是对象的引用.如果引用的元素是可变的 ...
- http://blog.csdn.net/a9529lty/article/details/6454156
http://blog.csdn.net/a9529lty/article/details/6454156
- vi 新建编辑文件时报错 E212 can’t open file for writing
在vi修改防火墙配置时,不能够保存,报E212 can’t open file for writing错误. 网上大概给出了两种答案. 一是权限不够,可以用root权限事实,或者sudo 操作. 二是 ...
- 解决 java.lang.ClassNotFoundException配置文件出错的问题
出现的原因: 1.jar包没有导入 2.jar包有冲突 3.jar包没有同步发布到自己项目的lib目录中 解决方案: maven构建工程的方式:项目点击右键 点击 Properties 选择Deplo ...
- My SqL 语法大全
一.SQL速成 结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表.索引.字段等)的建立和操纵. 以下是一些重要的SQL快速参考,有关SQL的 ...