POJ 2392 Space Elevator DP
该题与POJ 1742的思路基本一致:http://www.cnblogs.com/sevenun/p/5442279.html(多重背包)
题意:给你n个电梯,第i个电梯高h[i],数量有c[i]个,但是每个电梯所在高度不能超过a[i].
求问,怎么样的建造方案能够使电梯能够达到最大高度
思路:首先,必然要使电梯按a[i]进行排序,a[i]最小的电梯先建造。例如,电梯1,只能在高度20以下建造,而电梯2能在高度50以下建造,我当然先建造电梯1,否则如果先建造电梯2,就会导致我建造的高度早早超过了20,这样就无法充分利用了电梯1。
动态规划,想到转移方程为d[i][j],前i个电梯建造到高度j时,第i个电梯所剩余多少个。
默认d[i][j]为-1,代表前i个电梯无法达到高度j。
对于d[i][j],如果前i-1个电梯的建造已能够达到高度j,那么到高度j自然就不需要第i个电梯,所以就剩余c[i]个电梯
如果前i-1个电梯的建造不能达到高度j,那么我自然就要利用第i个电梯看看是否能够达到高度j,所以d[i][j] = d[i][j-h[i]]-1。
滚动数组:由于n最大为400,且a[i]最大为40000,那么由上面的定义,那么数组肯定就要达到400*40000了,感觉内存不够了
所以观察方程可知,d[i][j]的计算只会涉及到前一行和当前行,所以可以利用滚动数组,从而减少内存的使用。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int K = 404;
const int H = 40005;
int n,d[2][H];
struct node
{
int a,c,h;
}w[K];
int cmp(node n1, node n2)
{
return n1.a < n2.a;
}
void solve()
{
int f = 1;
memset(d, -1, sizeof(d));
sort(w,w+n,cmp);
for(int j = 0; j*w[0].h <= w[0].a; j++)
d[0][j*w[0].h] = w[0].c - j;
for(int i = 1; i < n; i++)
{
d[f][0] = w[i].c;
for(int j = 1; j <= min(w[i].a, H); j++)
{
if(d[!f][j] >= 0) d[f][j] = w[i].c;
else if(j >= w[i].h) d[f][j] = d[f][j-w[i].h]-1;
else d[f][j] = -1;
d[!f][j] = -1;
}
d[!f][0] = -1;
f = !f;
}
int ans = 0;
for(int i = w[n-1].a; i >= 0; i--)
{
ans = i;
if(d[!f][i]>= 0) break;
}
printf("%d\n", ans);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(~scanf("%d", &n))
{
for(int i = 0; i < n; i++)
scanf("%d %d %d", &w[i].h, &w[i].a, &w[i].c);
solve();
}
return 0;
}
POJ 2392 Space Elevator DP的更多相关文章
- POJ 2392 Space Elevator(贪心+多重背包)
POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木.每种积木都有一个高度h[i],一个数量num ...
- 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 ...
- 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 贪心+dp
题目链接: http://poj.org/problem?id=2392 题意: 给你k类方块,每类方块ci个,每类方块的高度为hi,现在要报所有的方块叠在一起,每类方块的任何一个部分都不能出现在ai ...
- POJ 2392 Space Elevator 背包题解
多重背包.本题不须要二分优化.相对简单点.由于反复数十分小,小于10. 而添加一个限制每种材料的高度做法.假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了. 还有就是注意要排序,以限制高度 ...
- POJ 2392 Space Elevator(多重背包)
显然塔的总高度不会超过最大的a[i],而a[i]之前的可以到达的高度 是由a值更小的块组成,所以按照a从小到大的顺序去转移. 然后就是多重背包判断存在性了,几乎和coin那题一样. 数据没coin丧病 ...
- A - Space Elevator(动态规划专项)
A - Space Elevator Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- poj2392 Space Elevator(多重背包问题)
Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8569 Accepted: 4052 ...
随机推荐
- pyqt之倒计时例子
from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main(): a= ...
- Laravel-表单篇-零散信息
1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...
- Counting Squares_hdu_1264(矩阵).java
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [Redux] Extracting Container Components -- Complete
Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...
- c#中关于virtual,override和new的理解
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- mysql主从同步报错
主从不同步,经查看发现如下报错 Last_Errno: 1666 Last_Error: Error executing row event: 'Cannot execute statement: ...
- SearchFlight_Joker
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- vs2015体验
项目结构 bower.json Bower依据此文件安装需要的前端的包 package.json NPM依据此文件获取对应的包 project.json 包含用于NPM的"poststore ...
- Server.HTMLEncode用法
Server.HTMLEncode用法!! Server.HTMLEncode HTMLEncode 一.HTMLEncode 方法对指定的字符串应用 HTML 编码. 语法 Server.HTMLE ...
- IFS解惑
一.IFS 介绍 Shell 脚本中有个变量叫 IFS(Internal Field Seprator) ,内部域分隔符.完整定义是The shell uses the value stored in ...