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.
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.
* 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.
题目大意
有一群牛要上太空,他们计划建一个太空梯(用一些石头垒),他们有k种不同类型的石头,每一种石头的高度为h,数量为c,由于会受到太空辐射,每一种石头不能超过这种石头的最大建造高度a,求解利用这些石头所能修建的太空梯的最高的高度.
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.
题解
记忆化搜索+贪心。把两个数组写成bool型的了。哭瞎了,调了半个小时。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n,ans;
struct shi
{int h,a,c;} s[402];
int pd[40002],usd[40002];
bool kp(const shi &x,const shi &y)
{return x.a<y.a;}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d%d",&s[i].h,&s[i].a,&s[i].c);
sort(s+1,s+n+1,kp);
memset(pd,0,sizeof(pd));
pd[0]=1; ans=0;
for(int i=1;i<=n;i++)
{memset(usd,0,sizeof(usd));
for(int j=s[i].h;j<=s[i].a;j++)
{if((!pd[j]) && pd[j-s[i].h] && usd[j-s[i].h]+1<=s[i].c)
{pd[j]=1;
usd[j]=usd[j-s[i].h]+1;
if(j>ans) ans=j;
}
}
}
printf("%d\n",ans);
return 0;
}