Result: Accepted
Time: 3ms
Memory: 1780kB
#include <cstdio>
#include <iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<stack>
using namespace std;
int n,m;
int i,j;
bool cmp(const char a,const char b){
return a<b;
}
struct node{
int h;
int w;
}book[1145];
int s[1145][3];
int main()
{
scanf("%d",&n);
int sum=1;
scanf("%d %d",&book[1].h,&book[1].w);
s[1][1]=book[1].h,s[1][2]=book[1].w;
for(i=2;i<=n;++i){
scanf("%d %d",&book[i].h,&book[i].w);
int flag=0;
for(j=1;j<=sum;++j)
{
if((book[i].h<=s[j][1]&&book[i].w<=s[j][2])||(book[i].h<=s[j][2]&&book[i].w<=s[j][1])){
flag=1;
if(book[i].h<=s[j][1]&&book[i].w<=s[j][2]){
s[j][1]=book[i].h,s[j][2]=book[i].w;
}
else if(book[i].h<=s[j][2]&&book[i].w<=s[j][1]){
s[j][1]=book[i].w,s[j][2]=book[i].h;
}
}
if(flag) break;
}
if(!flag)
{
s[sum+1][1]=book[i].h,s[sum+1][2]=book[i].w;
sum++;
}
}
printf("%d\n",sum);
return 0;
}