POJ1065

显然这是一个要寻找所给的木棍经过任意排序后最少能分成多少个不下降子序列的问题。但是因为有两个维度,因此很难做。我们可以先按一个键值进行从小到大排序,然后在剩下的序列中再寻找不下降的子序列。这个可以根据diworth定理在nlogn的时间内完成。考虑到lower_bound的查找特性,我们不妨从大到小排列然后再查找(也就是找反面)。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int f[5005];
struct wood{
    int l,w;
}G[5005];
bool cmp(wood a,wood b)
{
    if(a.l==b.l)
        return a.w>b.w;
    return a.l>b.l;
}
int main()
{
    int t,i,j,n;
    cin>>t;
    while(t--){
        cin>>n;
        for(i=1;i<=n;i++){
            scanf("%d%d",&G[i].l,&G[i].w);
        }
        sort(G+1,G+1+n,cmp);
        memset(f,0x3f,sizeof(f));
        for(i=1;i<=n;i++){
            *lower_bound(f+1,f+1+n,G[i].w)=G[i].w;
            //*k=G[i].w;
        }
        int len=lower_bound(f+1,f+1+n,0x3f3f3f3f)-f-1;
        cout<<len<<endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/humveea6/article/details/79898011