1. 程式人生 > >Poj 2392 Space Elevator

Poj 2392 Space Elevator

Space Elevator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12308 Accepted: 5854

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.

Source

題意: 一群牛想要上太空,有一些磚塊,用來搭建上太空的路,因為宇宙射線線的存在,每塊磚都有其最高限度,不能超過其最高限度。

           給你這些磚塊的高度, 最大限度,數量。

思路:Dp + 貪心,  先按照最大限度排序(升序),然後列舉每一類磚塊,在已存在高度繼續往上壘的情況,不斷記錄下下來。

程式碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef struct block{
	int h, a, c;// 高度 ,限度,數量 
}B;
B a[410];
bool dp[40100];
// dp[i] == true 高度 i 存在 
bool cmp(B m,B n)
{
	return m.a < n.a;
}
int main()
{
	int k;
	scanf("%d", &k);
	int max = 0;
	for(int i=1; i<=k; i++)
	{
		scanf("%d %d %d",&a[i].h, &a[i].a, &a[i].c);
	}
	
	sort(a+1, a+k+1, cmp); // 按照限度排序 
	memset(dp, 0, sizeof(dp));
	dp[0] = 1;
	
	for(int i=1; i<=k; i++)
	{
	     for(int j=a[i].a; j>=0; j--) 
		 // 從每一類磚的限度往下列舉 ,如果從0往上需要 01 陣列 
	     { 	
	     	 if(dp[j])  
	     	 {
                  for(int l=1; l<=a[i].c; l++)
                  {
                  	   int tem = j + a[i].h * l;
                  	   if(tem <= a[i].a)
                  	   {
                  	        dp[tem] = 1;	
					   }
				  }
             }
		 }
    }
    
    int ans = 0;
    for(int i=0; i<=a[k].a; i++)
    {
    	if(dp[i])  ans = i;
	}
    printf("%d\n", ans);
	return 0;
}


相關推薦

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

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

Poj 2392 Space Elevator

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

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(帶限制條件的多重揹包)

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

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 / 體積不定的多重揹包

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

poj 2392 Space Elevator(多重揹包)

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

POJ 2392 Space Elevator(多重揹包,排序)

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

POJ - 2392Space Elevator (dp,優秀的揹包問題)

題幹: 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 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

[完全揹包dp]Space Elevator POJ

poj.org/problem?id=2392 有一群奶牛想到太空去,他們有k中型別的石頭,每一類石頭高h,石頭能達到的高度c,以及它的數量a,在做揹包前需要對石塊能到達的最大高度(a)進行排序,而且每種磚塊都有一個限制條件,就是說以該種磚塊結束的最大高度H不能超過某個高

POJ Space Elevator

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) di

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

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

BZOJ1739: [Usaco2005 mar]Space Elevator 太空電梯

ret break blog con nbsp close ++ lap bre n<=400個東西,每個東西有高度<=100,這種東西在堆放過程中不得超過的最大高度<=40000,以及每個東西的個數<=10,求最高能堆多高。 算了下背包復雜度不太對

Space Elevator [POJ2392] [DP][優化]

for sort div 使用 輸入 cout 不能 space ace 題目大意 n件物品,第i件hi高,有ci件,最高的一件不能超過ai的高度。問最高能堆多高 輸入: 第一行,一個n 接下來每一行,為hi,ai,ci 輸出,最高堆多高 樣例輸入: 37 4

POJ 1696 Space Ant (極角排序)

img NPU values red 極角排序 {} num clock cts 題目: Description The most exciting space discovery occurred at the end of the 20th century. In

POJ 2392 多重揹包

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 &l

POJ 1696 Space Ant(極角排序)【計算幾何】

Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2489 Accepted: 1567 Description The most exci

POJ 2392 初涉多重揹包

題意:有K種石頭,每種石頭的高度為H,個數為Q,放置高度不能超過C。問這些石頭最高的可達高度是多少。 裸的多重揹包。 對於N種,K件的多重揹包可以轉換成∑ki的01揹包。 假設不存在限制C時,則先放A種還是先放B中對結果無影響。當新增上限制條件C時,應該讓C小的在下面。故在