10396: H.Rectangles
Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status][Web Board]
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
HINT
Source
题解:矩形嵌套数目,只需要把x从小到大排列,找lis就好了;注意x要比y小,lis要upper;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
struct Node{
int x,y;
friend bool operator < (Node a,Node b){
if(a.x!=b.x)return a.x<b.x;
else return a.y<b.y;
}
};
Node d[110],dt[110];
int main(){
int T,N;
SI(T);
while(T--){
SI(N);
int x,y;
for(int i=0;i<N;i++){
scanf("%d%d",&x,&y);
d[i].x=min(x,y);d[i].y=max(x,y);
}
sort(d,d+N);
int k=1;
dt[0].x=d[0].x;dt[0].y=d[0].y;
if(N==0){
puts("0");continue;
}
for(int i=1;i<N;i++){
while(d[i].x==d[i-1].x&&d[i].y==d[i-1].y)i++;
dt[k++]=d[i];
}
/*for(int i=0;i<k;i++){
printf("%d %d\n",dt[i].x,dt[i].y);
}*/
vector<int>vec;
for(int i=0;i<k;i++){
if(upper_bound(vec.begin(),vec.end(),dt[i].y)==vec.end())
vec.push_back(dt[i].y);
else *upper_bound(vec.begin(),vec.end(),dt[i].y)=dt[i].y;
} printf("%d\n",vec.size());
}
return 0;
}