poj 3636

Dilworth定理及其对偶定理的应用,定理的讲解请看如下博客

http://blog.csdn.net/xiaohuan1991/article/details/6956629

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 123456789
struct node
{
int x,y;
};
node vert[20001];
int res[20001];
int cmp(node a,node b)
{
if(a.x == b.x) return a.y > b.y;
return a.x < b.x;
}
int find(int left,int right,int k)
{
int mid;
while(left < right)
{
mid=(left + right) >> 1;
if(res[mid] >= k)
left = mid+1;
else
right=mid;
}
return left;
}
int work(int n)
{
int i,pos,len=1;
for(i=0;i<n;i++)
{
pos=find(0,len,vert[i].y);
if(pos==len)
res[len++]=vert[i].y;
else
res[pos]=vert[i].y;
}
return len;
}
int main()
{
int i,m,n;
scanf("%d",&n);
while(n--)
{
memset(res,0,sizeof(res));
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d %d",&vert[i].x,&vert[i].y);
sort(vert,vert+m,cmp);

printf("%d\n",work(m));
}
return 0;
}



posted @ 2012-03-23 13:29  书山有路,学海无涯  阅读(245)  评论(0编辑  收藏  举报