您的位置:首页 > 其它

【第三弹】【POJ2392】【Space Elevator】【解法一】

2015-07-31 20:18 344 查看
Space Elevator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9477Accepted: 4494
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

USACO 2005 March Gold

1. 最大的高度只要在for v的上限限制就好了。

2.如果有n1,n2两个东西,那么x1 <= min(n1.a-n1.h,n2.a-n1.h-n2.h) x2 <=min(n2.a-n2.h,n1.a-n1.h-n2.h)

所以x1 <= n2.a x2 <= n1.a

如果n1.a < n2.a 那么x1 可以包括x2的情形所以嗯。 就这样排序了。

#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
int K;
struct Node
{
	int h,a,c;
};
bool cmp(const Node&n1,const Node&n2)
{
//	return (min(n1.a-n1.h,n2.a-n1.h-n2.h)) > (min(n2.a-n2.h,n1.a-n1.h-n2.h));
	return n1.a < n2.a;
}
const int maxn = 410;
Node node[maxn];
const int maxv = 40010;
int dp[maxv];
int nums[maxv];
int n;
int main()
{

    while(scanf("%d",&K) != EOF)
	{
		n = -1;
		memset(dp,0,sizeof(dp));
		cout << K <<endl;
		for(int i=0;i<K;i++)
		{
			scanf("%d%d%d",&node[i].h,&node[i].a,&node[i].c);
			cout << node[i].h << " " << node[i].a  << " " << node[i].c <<endl;
			n = max(n,node[i].a);
		}
		sort(node,node+K,cmp);

		for(int i=0;i<K;i++)
		{
			memset(nums,0,sizeof(nums));
			for(int j=node[i].h;j<=node[i].a;j++)
			{
				if(dp[j] < dp[j-node[i].h] + node[i].h && nums[j-node[i].h] < node[i].c)
				{
					dp[j] =  dp[j-node[i].h] + node[i].h ;
					nums[j] = nums[j-node[i].h] + 1;
				}
			}
		}
		int maxs = -1;
		for(int i=n;i>=0;i--)
		{
			maxs = max(maxs,dp[i]);
		}
		printf("%d\n",maxs);
	}
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: