Dilworth 定理 偏序集的最小链划分=最长反链
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<utility>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
const int maxn = 211111;
pii p[maxn];
int y[maxn];
int x[maxn];
int binary(int l,int r,int k)
{
int ans=-1,m;
while(l<=r)
{
m=(l+r)>>1;
if(k>=x[m])
ans=m,r=m-1;
else
l=m+1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int n,m,P,i;
while(~scanf("%d%d%d",&n,&m,&P))
{
for(i=0;i<P;++i)
{
scanf("%d%d",&p[i].first,&p[i].second);
}
sort(p,p+P);
int l,r,t;
l=r=0;
for(i=0;i<P;++i)
{
y[i]=p[i].second;
if(l==r)
x[r++]=y[i];
else
{
t=binary(l,r-1,y[i]);
if(t<0)
x[r++]=y[i];
else
x[t]=y[i];
}
}
printf("%d\n",r);
}
return 0;
}