"Hdu 1069" Monkey and Banana (DP)

Source: Internet
Author: User
Monkey and Banana
A group of researchers are designing an experiment to test the IQ of monkeys. They will hang bananas on the roof of the building, while providing some bricks to these monkeys.   If the monkey is smart enough, it should be able to build a tower by reasonably placing some bricks and climb up to eat their favorite banana. The researchers have n types of bricks, each of which has an infinite number of bricks. The length and width height of block I bricks are indicated by Xi,yi,zi.   At the same time, because the bricks can be rotated, the 3 sides of each brick can make up 6 different lengths of height and width.   When building towers, a bricks can be placed above the B bricks only if the length and width of a bricks are smaller than the length and width of the B bricks respectively, because there must be room for the monkeys to step on. Your task is to write a program that calculates the height of the bricks that the monkeys can pile up. The input file contains multiple sets of test data. The first line of each test case contains an integer n, representing the number of different kinds of bricks. N<=30. The next n rows, 3 numbers per line, indicate the length and width of the bricks. When n= 0, you don't have to output any answers, and the test ends. Output maximum height for each set of test data. Format: Case Group data: maximum height = maximum height Sample Input 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 Sample Output case 1:maximum height = 2:maximum height = 21
Case 3:maximum height = 28
Case 4:maximum height = 342

Code:
/* When satisfying brick[i].x>brick[j].x&&brick[i].y>brick[j].y, there is Dp[i]=max (DP[I],DP[J]+BRICK[I].Z); 
Update the ANS value at any time; */#include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std
; struct Brick {int x, y, Z;} 
BRICK[200];  int dp[200];
The highest height of int ant after block I bricks is formed;
	void Store (int x,int y,int z) {brick[ant].x=x;
	Brick[ant].y=y;  Brick[ant++].z=z; 
	
	Must remember to ant++;
	Brick[ant].x=x;
	Brick[ant].y=z;
	
	Brick[ant++].z=y;
	Brick[ant].x=y;
	Brick[ant].y=x;
			
	Brick[ant++].z=z;
	Brick[ant].x=y;
	Brick[ant].y=z;
	
	Brick[ant++].z=x;
	Brick[ant].x=z;
	Brick[ant].y=x;
	
	Brick[ant++].z=y;
	Brick[ant].x=z;
	Brick[ant].y=y;			
Brick[ant++].z=x;
	} BOOL CMP (Brick A,brick b) {if (a.x==b.x) return a.y<b.y;
else return a.x<b.x;
	} int main () {int n;
	int case=1;                while (~SCANF ("%d", &n) &&n) {int ans=0; 
		Store answers;
 		Ant=1;
	   	int x, y, Z; for (int i=1;i<=n;i++) {scanf ("%d%d%d", &x,&y,&amP;Z);        Store (x, y, z); 
		Depositing a brick into an array in six different forms;
		} ant--;
		Sort (brick+1,brick+ant+1,cmp);    for (int i=1;i<=ant;i++) {dp[i]=brick[i].z; Initial assignment for (int j=1;j<i;j++) {if (BRICK[I].X&GT;BRICK[J].X&AMP;&AMP;BRICK[I].Y&GT;BRICK[J].Y) Dp[i]=max (d P[I],DP[J]+BRICK[I].Z); The function of//max is to find the highest height, because brick[i] is constant at I, but after sorting it out
                                                  There is no guarantee that the brick[j+1].z must be greater than brick[j].z, so when J increases there is no guarantee that the dp[j]+brick[i].z must be greater than the previous state Dp[i]

				Ans=max (ans,dp[i);	Ibid	
			}
		}
		printf ("Case%d:maximum height =%d\n", Case++,ans);
	}
	return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.