Description
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.
Input
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
Output for each test case , a single line with a integer K , the length of the longest sequence of fitting rectangles.
Sample Input
1
4
8 14
16 28
29 12
14 8
Sample Output
2
和南阳OJ上的矩形嵌套几乎一样。改改条件就AC了
。。。
#include<stdio.h>
#include<stdlib.h>
#define max(a,b) a>b?a:b
struct Node
{
int x;
int y;
} s[1001];
int com(const void *a,const void *b)
{
struct Node *c=(Node *)a;
struct Node *d=(Node *)b;
if(c->x==d->x)
return c->y-d->y;
return c->x-d->x;
}
int main(void)
{
int N;
scanf("%d",&N);
while(N--)
{
int dp[1001];
int i,j;
int n;
int a,b;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
if(a>b)
{
s[i].x=a;
s[i].y=b;
}
else
{
s[i].x=b;
s[i].y=a;
}
}
qsort(s,n,sizeof(s[0]),com);
for(i=0; i<n; i++)
{
dp[i]=1;
for(j=0; j<i; j++)
{
if(s[j].y<s[i].y&&s[j].x<s[i].x||s[j].y==s[i].y&&s[j].x<s[i].x||s[j].y<s[i].y&&s[j].x==s[i].x)
dp[i]=max(dp[i],dp[j]+1);
}
}
int max=0;
for(i=0; i<n; i++)
if(max<dp[i])
max=dp[i];
printf("%d\n",max);
}
return 0;
}