POJ_2392_Space_Elevator_(动态规划,背包)
描述
http://poj.org/problem?id=2392
磊方块,每种方块有数量,高度,以及该种方块所能处在的最高高度.问最高磊多高?
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10511 | Accepted: 4997 |
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.
Source
分析
看起来很像多重背包问题,只不过这里的重量和价值是一样的,并且多了一个所能处在的最高高度的限制.
考虑这个限制,设i种所能处在的最高高度为hi.对于两种方块i,j,设hi<hj.那么放的时候肯定是先放i比较好,因为如果先放了j的话,i只能放在j的上面,对于一种合法的情况,我们把在下面的h较大的,和在上面的h较小的调换位置是可以的,但是把在下面的h较小的和在上面的h较大的调换位置却不一定可以,所以应该尽量避免h小的放在上面,所以要先放i.放第i种方块的时候背包的容量上限是hi.
注意:
1.数组大小,好几次都没开够,没有好好看数据范围啊...
#include <cstdio>
#include <iostream>
#include <algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define read(a) a=getnum()
using namespace std; const int maxn=+,maxh=+;
int n;
int dp[maxh];
struct node { int h,v,m; }a[maxn]; inline int getnum(){int r=;char c;c=getchar();while(c<''||c>'')c=getchar();while(c>=''&&c<=''){r=r*+c-'';c=getchar();}return r;} bool comp(node x,node y) { return x.h<y.h; } void solve()
{
sort(a+,a+n+,comp);
int ans=;
for1(i,,n)
{
for(int k=;k<a[i].m;k*=)
{
for(int j=a[i].h;j>=k*a[i].v;j--)
{
dp[j]=max(dp[j],dp[j-k*a[i].v]+k*a[i].v);
ans=max(ans,dp[j]);
}
a[i].m-=k;
}
for(int j=a[i].h;j>=a[i].m*a[i].v;j--)
{
dp[j]=max(dp[j],dp[j-a[i].m*a[i].v]+a[i].m*a[i].v);
ans=max(ans,dp[j]);
}
}
printf("%d\n",ans);
} void init()
{
read(n);
for1(i,,n)
{
read(a[i].v);
read(a[i].h);
read(a[i].m);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("space.in","r",stdin);
freopen("space.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("space.out");
#endif
return ;
}
POJ_2392_Space_Elevator_(动态规划,背包)的更多相关文章
- Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp
1042: [HAOI2008]硬币购物 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1747 Solved: 1015[Submit][Stat ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- hdu 3008:Warcraft(动态规划 背包)
Warcraft Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- POJ_2184_Cow_Exhibition_(动态规划,背包)
描述 http://poj.org/problem?id=2184 n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大. Cow Exhibi ...
- Contest1874 - noip基础知识五:动态规划(背包、树dp、记忆化、递推、区间、序列dp、dp优化)
传送门 T1 dp[n][m]=dp[n-1][m-1]+dp[n-m][m] T2 ans=cat(n)*(n!)2 卡特兰数 T3 dp[i][j]=sigma(dp[i-1][j-a[i ...
- BZOJ1222 [HNOI2001]产品加工 - 动态规划- 背包
题解 怎么看都不像是个背包,直到我看了题解→_→, 第一次碰到这么奇怪的背包= = 定一个滚动数组$F_i$, $i$表示机器$a$用了$i$的时间, $F_i$表示机器$b$用了$F_i$的时间, ...
- 【洛谷】【动态规划/背包】P1417 烹调方案
由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的食物准备填 ...
- 【洛谷】【动态规划/背包】P1833 樱花
[题目描述:] 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏樱花:一种樱花树看一遍过,一种樱花树最多看Ai遍,一种樱花树 ...
- [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)
题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...
随机推荐
- 注释玩转webapi
using System; using System.Collections.Generic; using System.Net.Http.Formatting; using System.Web.H ...
- java新手笔记22 接口示例2
1.USB package com.yfs.javase; public interface USB { //定义规范 public void read(); public void write(); ...
- javascript Object的长度
1.示例 var obj = { a:"hello", b:"world", c:"hehe" } var key = 0; for(var ...
- javascript变量,类型 第9节
1.变量,数据类型 <html> <head> <title>变量</title> </head> <body> <h3& ...
- 使用ibatis时 sql中 in 的参数赋值
一.问题描述: 1.在使用ibatis执行下面的sql: update jc_jiesuan set doing_time = unix_timestamp(curdate()),doing_stat ...
- UVA 10795 A Different Task(汉诺塔 递归))
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...
- itoa : Convert integer to string
Quote from: http://www.cplusplus.com/reference/cstdlib/itoa/ function Required header : <s ...
- 利用iptables来配置linux禁止所有端口登陆和开放指定端口
from:http://www.myhack58.com/Article/sort099/sort0102/2011/31781.htm 1.关闭所有的 INPUT FORWARD OUTPUT 只对 ...
- linux运维工程师,必须掌握以下几个工具
本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题了 ...
- html定义对象
<object>定义一个对象<param>为对象定义一个参数 参数的名称:name = "" 参数的值:value=""classid: ...