Longest ascending subsequence (rectangle nesting)

Source: Internet
Author: User

Question one:

Rectangle nesting problem: given n rectangles, each rectangle has a length and width, which can be nested inside another rectangle only if the width of one rectangle is less than the length of the other rectangle. Find as many rectangles as you like, with the largest number of nested rectangles.

The problem is to ask for the longest ascending sub-sequence, except when the size of the comparison becomes a comparison of two variables.

#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN =1003;structRect {intL, W; BOOL operator< (ConstRect &b)Const {        returnW < B.W && L <B.L; }};BOOLcmpConstRect &a,ConstRect &b) {    if(A.L! =B.L)returnA.L <B.L; returnA.W <B.W;} Rect R[MAXN];intDP[MAXN];intMain () {intT, N; CIN>>T;  while(t--) {cin>>N;  for(inti =0; I < n; i++) {cin>> R[I].L >>R[I].W; if(R[i].l <r[i].w) Swap (R[I].L, R[I].W); Dp[i]=1; } sort (R, R+N, CMP); intAns =1;  for(inti =1; I < n; i++)        {             for(intj =0; J < I; J + +)            {                if(R[j] < R[i] && Dp[i] < Dp[j] +1) {Dp[i]= Dp[j] +1; }} ans=Max (ans, dp[i]); } printf ("%d\n", ans); }    return 0;} 
View Code

Question two:

Babel: Given n cubes, the selected cubes are stacked into a column as high as possible, requiring the length and width of each cube's bottom face to be strictly smaller than the length of the cube below him.

This problem is a little bit of a transformation is the longest ascending sub-sequence, but the input cube as three kinds of cubes on the line. When comparing the size, the same is the length and width. The specific code is as follows:

#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN = -;structRect {intL, W, H; BOOL operator< (ConstRect &b)Const {        return(L < B.L && W <B.W); }    voidFintAintBintc) {L= A; W = b; h =C; }    voidExchange () {if(L <W) Swap (L, W); }};rect R[MAXN];intDP[MAXN];BOOLcmpConstRect &a,ConstRect &b) {    if(A.L = =B.L)returnA.W >B.W; returnA.L >B.L;}intMain () {intN, Kase =0;  while(~SCANF ("%d", &n) &&N) {intA, B, c, cnt =0;        RECT tmp;  for(inti =0; I < n; i++) {scanf (" %d%d%d", &a, &b, &c);//Convert this cube into three cubes, with a height of a, B, C , respectively.Tmp.f (A, B, c); Tmp.exchange (); r[cnt++] =tmp; TMP.F (A, c, b); Tmp.exchange (); R[cnt++] =tmp; TMP.F (b, C, a); Tmp.exchange (); R[cnt++] =tmp; } sort (R, R+CNT, CMP); Memset (DP,0,sizeof(DP)); intAns =0;  for(inti =0; I < CNT; i++)//oldest -oldest sequence{Dp[i]=r[i].h;  for(intj =0; J < I; J + +)            {                if(R[i] <R[j]) dp[i]= Max (Dp[i], dp[j] +r[i].h); } ans=Max (ans, dp[i]); } printf ("Case %d:maximum height =%d\n", ++Kase, ans); }    return 0;}

Longest ascending subsequence (rectangle nesting)

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.