1. 程式人生 > >POJ 2392 Space Elevator (多重揹包 + 思路題)

POJ 2392 Space Elevator (多重揹包 + 思路題)

題目連結:http://poj.org/problem?id=2392

Space Elevator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7783 Accepted: 3690

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_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000). 

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K 

* 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

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

3
7 40 3
5 23 8
2 52 6

Sample Output

48

Hint

OUTPUT DETAILS: 

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.

題意:給你k種blocks,高度為hi,每種有ci個,約束條件是 想放置這個blocks的時候,當前總高度不超過ai。

分析:思考,如何將石塊排序,能保證做01揹包的正確性,有興趣的可以看這裡的第G題。

給程式碼具體去思考吧:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n;
class blocks{
public:
	int h,l;
	bool operator <(const blocks &b)const{
		return b.h+l<b.l+h;
	}
}a[4000];
int dp[40005];
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
void binary_div(int h,int limit,int num){
	int i = 1;
	while(num){
		if(num >= i){
			a[n].h = h*i;
			a[n++].l = limit;
			num -= i;
		}else{
			a[n].h = h*num;
			a[n++].l = limit;
			num = 0;
		}
		i <<= 1;
	}
}
int main(){
	int t,h,ai,num,m;
	while(scanf("%d",&t)!=EOF){
		m = 0;n = 0;
		for (int i = 0; i < t; i++)
		{
			scanf("%d %d %d",&h,&ai,&num);
			binary_div(h,ai,num);
			m = max(m,ai);
		}
		memset(dp,0,sizeof(dp));
		sort(a,a+n);//排序
		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			for (int j = a[i].l; j >= a[i].h; j--)
			{
				dp[j] = max(dp[j],dp[j-a[i].h]+a[i].h);
				ans = max(dp[j],ans);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

相關推薦

POJ 2392 Space Elevator 多重揹包 + 思路

題目連結:http://poj.org/problem?id=2392 Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3690

POJ 2392 Space Elevator多重揹包,排序

POJ 2392 Space Elevator 題目傳送門 題意: 你需要建一個高塔,材料總共有K種,每種材料有三個屬性:高度,數量,限度。限度是指該種材料只能在低於該限度的高度下被使用。問你最高能夠把這個高塔建到多高。 解題過程: 這題

POJ 2392 Space Elevator帶限制條件的多重揹包

大意:一群牛用石塊堆天梯,不同的石塊有不同的高度和最高的堆疊高度,求最終的高度。 分析:覺得是多重揹包,但是有了高度的限制。看了別人寫的程式碼半天才緩過來。啊,這樣處理。DP路漫漫。。 #inclu

Space Elevator多重揹包

Problem Description The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of bloc

poj 2392 Space Elevator 排序貪心+多重揹包 仍然很水 ★★

Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9955 Accepted: 4731 Description The cows are going to s

POJ-2392 Space Elevator 【動態規劃DP+多重揹包

題目傳送門 題目:牛要去太空了!他們計劃通過建造一種太空升降機來達到軌道:一個巨大的積木塔。他們有K (1 <= K <= 400)不同型別的積木來建造塔。型別i的每個塊的高度都是h_i (1 <= h_i <= 100),並且數量上都是c_i (1 <= c_

POJ 2392 Space Elevator / 體積不定的多重揹包

對於體積不變 可以先排個序 這樣對於體積大的物品 轉移時比他小的狀態都算出來了 #include <cstdio> #include <cstring> #include &l

poj 2392 Space Elevator(多重揹包

題意: k種石頭(不用在意具體是什麼東西),每種石頭的高度為h,這種石頭不能處於超過a的高度,數量為c,問最多能用這些石頭疊出多大的高度 解題思路: 這是一道稍微有點改動的多重揹包題目,被改為每種石頭都有一個容量限制。 多重揹包的問題我們可以進行轉換,對於c*h>=

POJ Space Elevator(排序+多重揹包)

題目分析 好久沒有寫揹包了,因此找了一個多重揹包練一下,因為多重揹包是01揹包和完全揹包的結合,手生,寫了有一會!!! 就是給你一下磚塊,每一個磚塊一個高度,給出數量和這樣磚塊最多能放的高

Cash Machine POJ 1276多重揹包——二進位制優化

題意:給你一個最大金額m,現在有n種類型的紙票,這些紙票的個數各不相同,問能夠用這些紙票再不超過m的前提下湊成最大的金額是多少? 題解:寫了01揹包直接暴力,結果T了,時間複雜度太高了,要跑外迴圈

Poj 2392 Space Elevator

Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12308 Accepted: 5854 Description The cows are going to

HDU-1059-Dividing多重揹包+二進位制優化

Problem DescriptionMarsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of t

PACM Team多重揹包+路徑記錄

正文: 所謂多重揹包,就是在其他屬性上有一些限制,除了空間可能還有其他的限制。這個其實就是幾個for迴圈掃一下就好了,幾個限制就開幾維陣列。 比如此題有四個限制 ,那麼就開四維陣列就好了 其中ma的意思是maxa a限制的最大值,mb、mc、md,同理。 fo

多重部分和問題多重揹包+二進位制優化

時間限制: 1 Sec  記憶體限制: 64 MB提交: 18  解決: 14 題目描述 有n種不同大小的數字,每種各個。判斷是否可以從這些數字之中選出若干使它們的和恰好為K。 輸入

POJ 3046 Ant Counting多重集組合數

Description Bessie was poking around the ant hill one day watching the ants march to and fro while gathering food. She realized that ma

POJ - 3740 Easy Finding Dance link 模板

 題意: 很裸的舞蹈鏈。 分析: 舞蹈鏈板題。 #include<cstdio> using namespace std; const int maxn=20*400; struct DLX{ int n,m,size; int U[max

POJ 2823 Sliding Window單調佇列入門

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 19088 Case Time Limit: 5000MS Description An array of size 

FZOJ1808 多米諾骨牌01揹包變形

其實剛開始看這道題的時候我覺得很難,在那裡想半天都想不通,最後還是看了其他大佬們的ac程式碼才恍然大悟 真吉二丟人,你AFO吧 咳咳,所以我們來看看 狀態: dp[i][j+k]=min(dp[i-1][j+k-(a[i]-b[i])],dp[

poj 2392Space Elevator貪心+多重揹包

Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13340 Accepted: 6323 Description The cows ar

poj Space Elevator 2392 多重揹包

Space Elevator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10212 Accepted: 4855 Description The cows are going to spac