POJ 1631 Bridging signals(LIS的等价表述)

把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述。

(如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样

O(nlogn)

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std;

const int maxn = 4e4+5;
int g[maxn];

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int T; cin>>T;
    while(T--){
        int n, ans = 0; scanf("%d",&n);
        for(int i = 0,c = 1; i < n; i++){
            int x, k; scanf("%d",&x);
            k = lower_bound(g+1,g+c,x)-g;
            ans = max(ans,k);
            g[k] = x;
            if(k==c) c++; //不用把辅助数组g初始化,只要维护一个下标即可
        }
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jerryRey/p/4887319.html