POJ 2392 Space Elevator(01背包)

来源:互联网 发布:新东方英语网络教学 编辑:程序博客网 时间:2024/06/16 11:16
Space Elevator
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 12063
Accepted: 5731

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

37 40 35 23 82 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

USACO 2005 March Gold
题目大意:
一群牛要上太空,给出n种石块,每种石块给出单块高度,总高度不能超过的最大值,数量,要求用这些石块能组成
的最大高度
解题思路:
先按高度最高值按从小到大排序,那么再运用01背包时可以得到最优结果
用dp[ i ]=1表示 i 高度是可以用石块组成的
sum[ i ]表示在用一种石块时,达到 i 高度需要的石块数
AC代码
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[40005],sum[40005];struct node{int h,a,c;}f[405];bool cmp(node a,node b){if(a.a<b.a)return true;return false;}int main(){int k;scanf("%d",&k);for(int i=0;i<k;i++){scanf("%d%d%d",&f[i].h,&f[i].a,&f[i].c);}sort(f,f+k,cmp);int ans=0;dp[0]=1;for(int i=0;i<k;i++){memset(sum,0,sizeof(sum));for(int j=f[i].h;j<=f[i].a;j++){if(!dp[j]&&dp[j-f[i].h]&&sum[j-f[i].h]<f[i].c){dp[j]=1;sum[j]=sum[j-f[i].h]+1;if(j>ans)ans=j;}}}printf("%d\n",ans);return 0;}