Rectangles
题目描述
Given N (4 <= N <= 100) rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence of K of the given rectangles that can "nest",(i.e., some sequence P1, P2, ..., Pk, such that P1 can completely fit into P2, P2 can completely fit into P3, etc.).
A rectangle fits inside another rectangle if one of its sides is strictly smaller than the other rectangle's and the remaining side is no larger. If two rectangles are identical they are considered not to fit into each other. For example, a 2*1 rectangle fits in a 2*2 rectangle, but not in another 2*1 rectangle.
The list can be created from rectangles in any order and in either orientation.
输入
The first line of input gives a single integer, 1 ≤ T ≤10, the number of test cases. Then
follow, for each test case:
* Line 1: a integer N , Given the number ofrectangles N<=100
* Lines 2..N+1: Each line contains two space-separated integers X Y, the sides of the respective rectangle. 1<= X , Y<=5000
输出
Output for each test case , a single line with a integer K , the length of the longest sequence of fitting rectangles.
样例输入
1
4
8 14
16 28
29 12
14 8
样例输出
2
#include<bits/stdc++.h>
using namespace std;
int dp[105];
struct rectangle
{int c,h;
}q[105];
int cmp(rectangle x,rectangle y)
{if(x.c<y.c)return 1;if(x.c==y.c&&x.h<=y.h)return 1;return 0;
}
int check(int i,int j)
{if(q[i].c>q[j].c){if(q[i].h>=q[j].h){return 1;}}elseif(q[i].c==q[j].c){if(q[i].h>q[j].h){return 1;}}return 0;
}int main()
{int t,n,a,b,Max;scanf("%d",&t);while(t--){Max=0;scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d",&a,&b);q[i].c=min(a,b);q[i].h=max(a,b);dp[i]=1;}sort(q,q+n,cmp);//printf("%d %d\n",q[0].c,q[3].c);for(int i=0;i<n;i++)///后面的{for(int j=i-1;j>=0;j--)///前面的{if(check(i,j))dp[i]=max(dp[i],dp[j]+1);}Max=max(dp[i],Max);}printf("%d\n",Max);}return 0;
}