A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply ofblocks of each type. Each type-i block was a rectangular solid withlinear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the otherdimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placedon top of another block as long as the two base dimensions of theupper block were both strictly smaller than the corresponding basedimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized basescouldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
贪心?。
排序之后对于当前每次能达到的最大高度是在它之前的满足要求下(长和宽都小于当前积木)的最大高度加上自己的高度。
#include<cstdio> #include<algorithm> using namespace std; struct Node{ long long x,y; long long w,s; }num[300]; bool cmp1(Node a,Node b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; } int main() { long long n; long long i,j; long long count,t=1,tmp; while(scanf("%lld",&n),n) { count=0; for(i=1;i<=n;i++) { count++; scanf("%lld%lld%lld",&num[count].x,&num[count].y,&num[count].w); num[++count].x=num[count-1].y; num[count].y=num[count-1].x; num[count].w=num[count-1].w; num[++count].x=num[count-2].w; num[count].y=num[count-2].x; num[count].w=num[count-2].y; num[++count].x=num[count-3].x; num[count].y=num[count-3].w; num[count].w=num[count-3].y; num[++count].x=num[count-4].y; num[count].y=num[count-4].w; num[count].w=num[count-4].x; num[++count].x=num[count-5].w; num[count].y=num[count-5].y; num[count].w=num[count-5].x; } sort(num+1,num+1+count,cmp1); for(i=1;i<=count;i++) num[i].s=num[i].w; long long max; for(i=1;i<=count;i++) { max=0; for(j=1;j<i;j++) { if(num[i].x>num[j].x && num[i].y>num[j].y && num[j].s>max) max=num[j].s; } num[i].s=num[i].w+max; } max=0; for(i=1;i<=count;i++) if(num[i].s>max) max=num[i].s; printf("Case %lld: maximum height = %lld\n",t++,max); } return 0; }