LIS (template)

Greedy + half
Apparently its the end of the element the smaller, the better then other elements in the back, the more likely to become longer
so we constantly update (answer) elements in a sequence, to make it more conducive to answer back

#include<bits/stdc++.h>
#define re return
#define inc(i,l,r) for(int i=l;i<=r;++i)
const int maxn=100005,maxm=2005;
using namespace std;
template<typename T>inline void rd(T&x)
{
    char c;bool f=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')f=1;
    x=c^48;
    while((c=getchar())>='0'&&c<='9')x=x*10+(c^48);
    if(f)x=-x;
}

int f[maxn],k,n;

int main()
{
    int x;
    rd(n);
    inc(i,1,n)
    {
        rd(x);
        if(x>f[k])f[++k]=x;
        else
        {
            int l=1,r=k;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(f[mid]<x)l=mid+1;
                else r=mid-1;
            }
            
            f[l]=x;
        } 
    }
    printf("%d",k);
}

Guess you like

Origin www.cnblogs.com/lsyyy/p/11285519.html