Nanyang ACM16 rectangular nested dynamic planning, Nanyang acm16

Source: Internet
Author: User

Nanyang ACM16 rectangular nested dynamic planning, Nanyang acm16
Rectangular nesting time limit: 3000 MS | memory limit: 65535 KB difficulty: 4

Description
There are n rectangles. Each rectangle can be described by a and B to indicate length and width. Rectangle X (a, B) can be nested in Rectangle Y (c, d) When and only when a <c, B <d or B <c, a <d (equivalent to rotating x 90 degrees ). For example, () can be nested in (), but cannot be nested in. Your task is to select as many rectangles as possible and arrange them in one row so that, except the last one, each rectangle can be nested in the next rectangle.
Input
The first line is a positive number N (0 <N <10), indicating the number of test data groups,
The first row of each group of test data is a positive number n, indicating the number of rectangles in the group of test data (n <= 1000)
The next n rows have two numbers a and B (0 <a, B <100), indicating the length and width of the rectangle.
Output
Each group of test data outputs one number, indicating the maximum number of rectangles that meet the conditions. Each group of output occupies one row.
Sample Input
1101 22 45 86 107 93 15 812 109 72 2
Sample output
5

Algorithm idea: first compare the values of a and B, put the larger values in c [I] [0], and the smaller values in c [I] [1], then sort c [I] [0] in ascending order. finally, we use dynamic planning. dp [I] <dp [j] + 1 depends on the code implementation:

# Include <stdio. h>
Int main ()
{
Int t;
Scanf ("% d", & t );
While (t --)
{
Int n, I, j, a, B, x, y, max;
Int c [1001] [2], dp [1001];
Scanf ("% d", & n );
// Place large values in c [I] [0] and small values in c [I] [1];
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a, & B );
C [I] [0] = a> B? A: B;
C [I] [1] = a <B? A: B;
}
// Sort the two-dimensional array in ascending order
For (I = 0; I <n-1; I ++)
For (j = I + 1; j <n; j ++)
{
If (c [I] [0]> c [j] [0])
{
X = c [I] [0];
C [I] [0] = c [j] [0];
C [j] [0] = x;

Y = c [I] [1];
C [I] [1] = c [j] [1];
C [j] [1] = y;
}
}
// Use dynamic planning to find the maximum value
For (I = 0; I <n; I ++)
{
Dp [I] = 1;
For (j = 0; j <I; j ++)
{
If (c [I] [0]> c [j] [0] & c [I] [1]> c [j] [1] & dp [I] <dp [j] + 1)
Dp [I] = dp [j] + 1;
}
}
Max = dp [0];
For (I = 0; I <n; I ++)
If (max <dp [I])
Max = dp [I];
Printf ("% d \ n", max );
}
Return 0;
}

Related Article

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.