poj 动态规划DP - 2392 Space Elevator

背包九讲里面的多重背包问题。每一块积木有高度和数量,也有最大能到的高度位置限制。我们DP前首先要对高度限制进行排序,因为从直观角度说,我们也需要先处理较小的能到高度的积木,这样才能取到最大值。例如第一个高度是11,限制高度是100,第二个的限制高度是10,那如果直接处理,第二个不会被处理到。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define max(x,y)(x>y?x:y)
struct block
{
	int h;
	int a;
	int c;
};
struct block data[410];
int  dp[40010]={0};
int cmp(const void*p1,const void*p2)
{
return (*( block*)p1).a > (*( block*)p2).a?1:-1;
}
int main(){
	int k,i,j,m;
	scanf("%d",&k);
	for(i=0;i<k;i++){
		scanf("%d %d %d",&data[i].h,&data[i].a,&data[i].c);
	}
	qsort(data,k,sizeof(data[0]),cmp);
	memset(dp,0,sizeof(dp));
	for(i=0;i<k;i++){
		for(m=1;m<=data[i].c;m++)
			for(j=data[i].a;j>=data[i].h;j--)
				dp[j]=max(dp[j],dp[j-data[i].h]+data[i].h);
	}
	int ans=0;
	for(i=0;i<=data[k-1].a;i++) ans=max(ans,dp[i]);
	printf("%d\n",ans);
	return 0;
}
时间: 2024-08-02 03:53:24

poj 动态规划DP - 2392 Space Elevator的相关文章

POJ 2392 Space Elevator(贪心+多重背包)

POJ 2392 Space Elevator(贪心+多重背包) http://poj.org/problem?id=2392 题意: 题意:给定n种积木,每种积木都有一个高度h[i],一个数量num[i],还有一个限制条件,这个积木所在的位置不能高于limit[i],问能叠起的最大高度? 分析: 本题是一道多重背包问题, 不过每个物品的选择不仅仅要受该种物品的数量num[i]限制, 且该物品还受到limit[i]的限制. 这里有一个贪心的结论: 我们每次背包选取物品时都应该优先放置当前limi

POJ 2392 Space Elevator 背包题解

多重背包,本题不需要二分优化.相对简单点.因为重复数十分小,小于10: 而增加一个限制每种材料的高度做法,如果使用逆向填表,那么只需要从这个高度往小递归填表就可以了. 还有就是注意要排序,以限制高度为标准从小到大排序,否则答案错误的. #include <stdio.h> #include <string.h> #include <algorithm> using std::sort; const int MAX_K = 401; const int MAX_H = 4

poj 2392 Space Elevator(多重背包+先排序)

Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h

poj 2392 Space Elevator (多重背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8110   Accepted: 3843 题目大意  :一群牛要上天  用一些石块堆塔  给出石块的种类  及其每个种类的数量 和该种石块能出现的最高高度  和每种石块的数量 求怎么摆放才能堆得最高 多重背包模板题.... 将所有石块排序  把高度低的放下面 #include<iostream> #include<cstdio>

POJ 2392 Space Elevator(多重背包)

Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h

POJ 题目2392 Space Elevator(多重背包)

Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9042   Accepted: 4296 Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <

POJ 2392 Space Elevator 贪心+dp

题目链接: http://poj.org/problem?id=2392 题意: 给你k类方块,每类方块ci个,每类方块的高度为hi,现在要报所有的方块叠在一起,每类方块的任何一个部分都不能出现在ai以上的高度,问这些方块能叠的最高高度. 题解: 首先按ai升序排序,尽量让高度限制低的先排掉,如果不这样做一些转移会失效掉: 比如:h1=3,a1=3;h2=4,a2=7 如果先搭1再搭2则合法,但反过来则变成无效的转移了. 处理好顺序之后跑一遍背包就可以了,因为最大高度为40000,比较小,所以用

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,就会导致我建造的高度早早超过

poj 2392 (Space Elevator) 1276 (Cash Machine)变形背包

这道题跟coins很像,看来楼教主的男人八题果然不简单. 进行coins式的背包处理就好了. 2392 #include<iostream> #include<algorithm> #include<string.h> #include<stdlib.h> #include<stdio.h> #define max(a,b) ((a)>(b)?(a):(b)) typedef long long ll; using namespace st