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 ...
随机推荐
- Traefik访问master节点不通的问题定位
问题 部署traefik到客户节点的对外访问节点后,发现日志里面报错 类似于 E0122 :: reflector.go:] k8s.io/dns/vendor/k8s.io/client-go/to ...
- 【Todo】Boost安装与学习
现在这里找下载包 http://sourceforge.net/projects/boost 我找的是 1_62_0 下面是从公司wiki上找到的一个说明. boost & thrift安装步 ...
- Parallax Occlusion Mapping in GLSL [转]
http://www.sunandblackcat.com/tipFullView.php?topicid=28 This lesson shows how to implement differ ...
- [PHP]如何使用Mobile_Detect来判断访问网站的设备:安卓,平板,电脑
Mobile_Detect 是一个轻量级的开源移动设备(手机)检测的 PHP Class, 它使用 User-Agent 中的字符串,并结合 HTTP Header,来检测移动设备环境. 这个设备检测 ...
- php 获取/设置用户訪问页面语言类
User Language Class 获取/设置用户訪问的页面语言,假设用户没有设置訪问语言.则读取Accept-Language. 依据用户选择的语言显示相应的页面(英文.中文简体,繁体中文) U ...
- 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结
不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...
- JS 数字左补零函数
/* 左边自动补零 质朴长存法 by lifesinger */ function pad(num, n) { var len = num.toString().length; while(len & ...
- 【树莓派】【转载】Raspberry Pi (树莓派)折腾记
在网上看到一篇对树莓派折腾记录比较详细的文章,时间比较早,但是有些东西没变. 对于新手而言,还是有点参考价值.文章参见:http://skypegnu1.blog.51cto.com/8991766/ ...
- hdu4515小Q系列故事——世界上最遥远的距离
Problem Description 世界上最遥远的距离 不是生与死 而是我就站在你面前 你却不知道我爱你 世界上最遥远的距离 不是我就站在你面前你却不知道我爱你 而是明明知道彼此相爱 却不能在一起 ...
- 算法笔记_167:算法提高 矩阵翻转(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 Ciel有一个N*N的矩阵,每个格子里都有一个整数. N是一个奇数,设X = (N+1)/2.Ciel每次都可以做这样的一次操作:他从矩阵 ...