poj3636
dilworth定理应用
#include <stdio.h>
#include <algorithm>
using namespace std;
int a,b,v[20000];
struct node
{
int w,h;
}m[20000];
bool cmp(node a,node b)
{
if(a.w==b.w) return a.h>b.h;
else return a.w<b.w;
}
int f()
{
int cnt=0;
for(int i=0;i<b;++i)
if(!v[i])
{
v[i]=1;
++cnt;
int tmp=m[i].h;
for(int j=i+1;j<b;++j)
if(!v[j]&&tmp<m[j].h)
{
v[j]=1;
tmp=m[j].h;
}
}
return cnt;
}
int main()
{
scanf("%d",&a);
for(int i=0;i<a;++i)
{
scanf("%d",&b);
for(int j=0;j<b;++j)
scanf("%d %d",&m[j].w,&m[j].h),v[j]=0;
sort(m,m+b,cmp);
printf("%d\n",f());
}
return 0;
}
二分加速(求解不上升子序列)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a,b,v[20000],len;
struct node
{
int w,h;
}m[20000];
bool cmp(node a,node b)
{
if(a.w==b.w) return a.h>b.h;
else return a.w<b.w;
}
int search(int key)
{
int a=0,b=len,t;
while(a<b)
{
t=(a+b)>>1;
if(v[t]<key) b=t;
else a=t+1;
}
return a;
}
int f()
{
len=0;
v[0]=m[0].h;
for(int i=1;i<b;++i)
{
int t=search(m[i].h);
if(v[t]<m[i].h) v[t]=m[i].h;
else v[++len]=m[i].h;
}
return len+1;
}
int main()
{
scanf("%d",&a);
for(int i=0;i<a;++i)
{
scanf("%d",&b);
for(int j=0;j<b;++j)
scanf("%d %d",&m[j].w,&m[j].h);
sort(m,m+b,cmp);
printf("%d\n",f());
}
return 0;
}
浙公网安备 33010602011771号