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 ...
随机推荐
- H5 manifest离线缓存
请跳转我的有道云笔记查看: http://note.youdao.com/noteshare?id=caaf067c6e38820ba8f87b212c2327a9&sub=23E0F8F7A ...
- 简单nginx+tomca负载均衡
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...
- Javascript中的对象(二)
Javascript是一种基于原型的对象语言,而不是我们比较熟悉的,像C#语言基于类的面向对象的语言.在前一篇文章中,我们已经介绍了Javascript中对象定义的创建.接下来我们来介绍一下Javas ...
- jstl标签不起作用不管用,jstl标签直接输出表达式
jstl标签不起作用不管用,jstl标签直接输出表达式 解决办法: 在jsp页面头上 引入: <%@ page isELIgnored="false"%> 便能够解决.
- 《Linux操作系统编译构建指南》
在线阅读地址:http://www.doc88.com/p-5126905896771.html Linux编译构建定制qq群: 521902245 文件夹...0 前言...3 第零章 绪论...5 ...
- 不输入sudo使用docker
系统是debian系 安装: sudo apt install docker.io 将当前用户加入‘docker’组: sudo gpasswd -a ${USER} docker 刷新权限: su ...
- MySQL数据库如何与EXCEL的XLS格式相互转换
1 将SQL导出为EXCEL方法,有如下数据库(my_impa),里面有两张表 2 如果是EXCEL格式,一定要勾选"将字段名称放在首行",否则待会儿导入的时候就需要你手工新建字段 ...
- xcode 修改 organization name 和 company identifier
一:修改 organization name 在终端下 defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ORGA ...
- 算法笔记_065:分治法求逆序对(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 分治法(归并排序) 1 问题描述 给定一个随机数数组,求取这个数组中的逆序对总个数.要求时间效率尽可能高. 那么,何为逆序对? 引用自百度 ...
- 详细解说 STL 排序(Sort)(转)
作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...