Dag model (Rectangle nesting)

Source: Internet
Author: User

Recommended Online Example: http://acm.nyist.net/JudgeOnline/problem.php?pid=16

Title Excerpt:

Rectangle Nesting 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.
Input
The
first line is a positive positive N (0<N<10), which indicates the number of test data groups,
The first row of each set of test data is a positive positive n, indicating the number of rectangles in the group's test data (n<=1000)
The next n rows, each with two numbers, A, B (0<a,b<100), represent 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 set of outputs
Sample input
1101 22 45 86 107 93 15 812 109 72 2
Sample output
5

Print1 is the maximum number of rectangles to be output according to the topic
Print2 is to output the selected rectangle


#include"iostream"#include"Cstdio"#include"CString"#include"algorithm"using namespacestd;#defineMAXN 100intN,max;intDP[MAXN];structnode{intL,w;} MAT[MAXN];BOOLCMP (node A,node b) {if(A.L!=B.L)returna.l<B.L; returna.w<B.W;}intMaxintAintb) {    returnA>b?a:b;}voidSwapint&a,int&b) {    intT; T=A; A=b; b=t;}voidInit () {cin>>N; intTL,TW;  for(intI=1; i<=n;i++) {scanf ("%d%d",&tl,&TW); if(tl<tw) Swap (TL,TW); MAT[I].L=tl;mat[i].w=tw; } memset (DP,0,sizeof(DP));}voidWork () {Sort (Mat+1, mat+1+n,cmp); Max=-1;  for(intI=1; i<=n;i++)   {        for(intj=1; j<=i;j++)       {          if(mat[i].l>mat[j].l&&mat[i].w>MAT[J].W) {Dp[i]=max (dp[i],dp[j]+1); }} Dp[i]=max (Dp[i],1); Max=Max (max,dp[i]); }}voidPrint1 () {printf ("%d\n", Max (1, Max));}voidPrint2 (inti) {printf ("%d", i); for(intj=1; j<=n;j++)if(dp[i]==dp[j]+1&AMP;&AMP;MAT[I].L&GT;MAT[J].L&AMP;&AMP;MAT[I].W&GT;MAT[J].W) {Print2 (j); Break;}}intMain () {intT; CIN>>T;  while(t--) {Init ();      Work ();      Print1 (); //Print2 (n);cout<<endl;    }    return 0;}
1

Dag model (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.