定理1 令(X,≤)是一个有限偏序集,并令r是其最大链的大小。则X可以被划分成r个但不能再少的反链。
其对偶定理称为Dilworth定理:
定理2 令(X,≤)是一个有限偏序集,并令m是反链的最大的大小。则X可以被划分成m个但不能再少的链。
链:集合中任意两个元素可比
反链:任意两个元素不可比
这题中,要求的是链的最小划分(当一个可以套住另一个时即为可比)
只需求出最大反链。
因为当一个节点的w,h和另一个节点的w,h不同时大于或者小于时算不可比
因此将其按w排序,使得排序完成后,让序列中每个节点,在他的h比下一个节点的h大则是可比的,否则是不可比的。
那么求出这个序列的LIS,这个LIS中所有数字代表的节点都是不可比的。那么这个LIS代表的节点即为最大反链。他的长度即为链的最小划分。由于只需要长度,我们可以用nlogn的算法求LIS的长度。
对了是最长不降子序列不是LIS……
#include<iostream>
#include<fstream>
#include<algorithm>
#include<queue>
#include<set>
#include<cstring>
#define ll long long
#define CLR(x) memset(x,0,sizeof(x))
#define forto(i,n) for(int i=0;i<n;i++)
#define for1to(i,n) for(int i=1;i<=n;i++)
using namespace std;
int LIS[22222];
int LISLen;
struct node
{
int w;
int h;
bool operator<(const node& Ano) const
{
return w!=Ano.w? w>Ano.w :h<Ano.h;///w=w时也是不可比的,因此要把大的h放到后面表示不可比。
}
}A[22222];
int main()
{
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif // ONLINE_JUDGE
int Total;
cin>>Total;
forto(Case,Total)
{
int n;
cin>>n;
forto(i,n)
cin>>A[i].w>>A[i].h;
sort(A,A+n);
LISLen=0;
LIS[LISLen++]=A[0].h;
for1to(i,n-1)
{
if (A[i].h>=LIS[LISLen-1])
LIS[LISLen++]=A[i].h;
else
*upper_bound(LIS,LIS+LISLen,A[i].h)=A[i].h; ///最长不降可以用upper_bound,最长上升可以用lower_bound
}
cout<<LISLen<<endl;
}
return 0;
}