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 ...
随机推荐
- Android内存优化3 了解java GC 垃圾回收机制1
开篇废话 如果我们想要进行内存优化的工作,还是需要了解一下,但这一块的知识属于纯理论的,有可能看起来会有点枯燥,我尽量把这一篇的内容按照一定的逻辑来走一遍.首先,我们为什么要学习垃圾回收的机制,我大概 ...
- 兄弟连新版Linux视频教程
兄弟连新版Linux视频教程目录:F:\linux\兄弟连新版Linux视频教程├(1)云计算是什么东西集群又是嘛玩意?.mp4├(2)兄弟连新版Linux教程 1.1.1 Linux系统简介-UNI ...
- python3将docx转换成pdf,html文件,pdf转doc文件
直接上代码 # -*- encoding:utf-8 -*- """ author:lgh 简单的doc转pdf,html,pdf转doc脚本 依赖库pdfminer3k ...
- Oracle imp关于fromuser 和 touser的用法
fromuser就是把当前的dmp文件中的某一个用户下的数据取出.touser就是把现在dmp文件中的数据导入到目标库的指定user下.具体命令这样.exp userid=system/manager ...
- SVN-两种存储方式的比较(BDB vs. FSFS)
Subversion 的版本库(repository),就是位于服务器端,统一管理和储存数据的地方.本文中,我们以 Linux 为例,介绍在服务器端配置和管理 Subversion 版本库的基本方法. ...
- ubuntu 的runlevel设定
修改ubuntu的启动级别 runlevel ----------------------------------------------------------------------------- ...
- 在Eclipse中点击ctrl+h打开搜索界面,范围指定的项目中搜索包含person的文件
ctrl+h ===>File Search Tab ===>在containing text里输入person,scope ==>selected resources,搜索就可以了
- html中锚点的应用【本页面跳转】
设置锚点 <a name="top"></a> 同页跳转 <a href="#top">返回顶部</a> 不同页 ...
- 怎样在model里面使用number_to_currency
ActiveSupport::NumberHelper.number_to_currency(amount, precision: 0)
- git for c#, clone方法
private static void clone() { string wkDir = @"E:\DotNet2010\单位project\Git.Client\lib2Test\Cons ...