Nyoj 16 Rectangular nesting (dynamic planning)

Source: Internet
Author: User

Time limit: ms | Memory limit:65535 KB

Difficulty:4

  • Describe

  • There are n rectangles, each of which can be described by a A, a, long and wide. A rectangle x (A, b) can be nested in the rectangle y (c,d) when and only if A<c,b<d or b<c,a<d (equivalent to rotation X90 degrees). For example (1,5) can be nested within (6,2), but not nested in (3,4). Your task is to select as many rectangles as possible, so that each rectangle can be nested within the next rectangle, except for the last one.

      • Enter

      • The first row is a positive positive number n (0 <N<10), which represents the number of test data groups,
        The first row of each set of test data is a positive positive n, which indicates the number of rectangles in the group test data (n<=1000)
        followed by n rows, each row has two numbers, A, B (0<a,b<100), Represents the length and width of the rectangle

      • Output

      • Each set of test data outputs a number that represents the maximum number of rectangles that meet the criteria, one row for each group of outputs

      • Sample input

      • Pre id= "Sample_input" style= "margin-top:0px;margin-bottom:0px;padding:5px 10px;font-family:consolas, ' Courier New ', ' Dejavu Sans mono ', ' Droid Sans Mono ', Monospace;background-color:rgb (239,239,239); border:1px solid RGB (204,204,204); line-height:1.5em; " >1101 22 45 86 107 93 15 812 109 72 2
      • Sample output

      • 5 
/*16. Rectangle nesting */#include <iostream> #include <algorithm>using namespace std;//rectangular structure, including   long, Wide typedef struct{int a;int b;} nrectangle;//Declaration Rectangle Array nrectangle r[1000];//Interchange Rectangle's length, Width void swap (int *a, int *b) {*a=*a^*b;*b =*a^*b;*a=*a^*b;} CMPBOOL&NBSP;CMP (NRECTANGLE&NBSP;C1,&NBSP;NRECTANGLE&NBSP;C2) {if (c1.a < c2.a)  return  True;if (c1.a==c2.a && c1.b <= c2.b) Return true;return false;} Int main () {int n,m,i,j,max,count;int dp[1000]={0};cin>>n;while (n--) {count = 1; cin>>m;//input m initial rectangle length, width for (i=0;i<m;i++) {cin>>r[i].a>>r[i].b;if (r[i].a<r[i].b) Swap (&r[ I].A,&AMP;R[I].B);} Sort (r,r+m,cmp);DP [0]=1;for (i=1;i<m;i++) {max=0;for (j=i-1;j>=0;j--) if (r[i].a>r[j].a && &NBSP;R[I].B&GT;R[J].B) if (Max < dp[j]) max=dp[j];DP [I]=max+1;if (Count<dp[i]) count=dp[i];} Cout<<count<<endl;} return 0;}


This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1619496

Nyoj 16 Rectangular nesting (dynamic planning)

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.