NYOJ 16 rectangular nesting

Source: Internet
Author: User


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
1
10
1 2
2 4
5 8
6 10
7 9
3 1
5 8
12 10
9 7
2 2
Sample output
5
Solution: 1. The longest ascending subsequence of DP
2. The rectangle can be rotated 90 degrees, so the length and width can be changed at will. Based on greedy ideas, we know that if we want to get the most rectangular nesting, so it is very important to sort the length and width, that is to say, to ensure that all the rectangles can be arranged in a sequence as much as possible, so that we can satisfy the most nested rectangles. So when I input data, let the big one take the long and small as the width, then we sort the length, and then find the longest ascending subsequence from the width.

Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <set>
Using namespace std;
# Define maxn1010
 
Int t, n;
Int l [MAXN], w [MAXN];
Int dp [MAXN];
 
Void solve (){
Int tmp_l [MAXN], tmp_w [MAXN];
Int vis [MAXN];

Memset (vis, 0, sizeof (vis ));
Memset (dp, 0, sizeof (dp ));
Memcpy (tmp_l, l, sizeof (tmp_l ));
// Sort and re-store
Sort (tmp_l, tmp_l + n );
For (int I = 0; I <n; I ++ ){
For (int j = 0; j <n; j ++ ){
If (tmp_l [I] = l [j] &! Vis [j]) {
Tmp_w [I] = w [j]; vis [j] = 1; break;
}
}
}
// Solves the longest ascending subsequence.
Int max_len = 1; // It must be initialized to 1
For (int I = 0; I <n; I ++ ){
Dp [I] = 1;
For (int j = I-1; j> = 0; j --){
If (tmp_l [I]> tmp_l [j] & tmp_w [I]> tmp_w [j] & dp [j] + 1> dp [I])
Dp [I] = dp [j] + 1;
If (max_len <dp [I]) max_len = dp [I];
}
}
Printf ("% d \ n", max_len );
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
Int a, B;
Scanf ("% d", & t );
While (t --){
Scanf ("% d", & n );
For (int I = 0; I <n; I ++ ){
Scanf ("% d", & a, & B );
If (a> B) {l [I] = a; w [I] = B ;}
Else {l [I] = B; w [I] = ;}
}
Solve ();
}
Return 0;
}


Author; cgl1079743846

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.