最长单调序列O(nlbn)

 

 

http://blog.sina.com.cn/s/blog_4e4148700100cxjm.html

const int MAX=2000000;
int a[MAX];
//求a【ll...rr】的最长严格单调递增序列长度O(nlbn)
int bb[MAX];
int lcs(int *a,int ll,int rr)
{
    int i,k=1;
    bb[1]=a[ll];
    for(i=ll+1;i<=rr;i++)
    {
        if(a[i]>bb[k]) bb[++k]=a[i];
        else
        {           
            int ll=1,rr=k,mid;
            while(ll
            {
                mid=(ll+rr)>>1;
                if(bb[mid]>=a[i]) rr=mid;
                else ll=mid+1;
            }bb[rr]=a[i];
        }
    } return k;
}

const int MAX=2000000;
int a[MAX];
//求a【ll...rr】的最长严格单调递减序列长度O(nlbn)
int bb[MAX];
int lcs(int *a,int ll,int rr)
{
    int i,k=1;
    bb[1]=a[ll];
    for(i=ll+1;i<=rr;i++)
    {
        if(a[i]
        else
        {         
            int ll=1,rr=k,mid;
            while(ll
            {
                mid=(ll+rr)>>1;
                if(bb[mid]<=a[i]) rr=mid;
                else ll=mid+1;
            }bb[rr]=a[i];
        }
    } return k;
}

const int MAX=2000000;
int a[MAX];
//求a【ll...rr】的最长单调非降序列长度O(nlbn)
int bb[MAX];
int lcs(int *a,int ll,int rr)
{
    int i,k=1;
    bb[1]=a[ll];
    for(i=ll+1;i<=rr;i++)
    {
        if(a[i]>=bb[k]) bb[++k]=a[i];
        else
        {           
            int ll=1,rr=k,mid;
            while(ll
            {
                mid=(ll+rr)>>1;
                if(bb[mid]>a[i]) rr=mid;
                else ll=mid+1;
            }bb[rr]=a[i];
        }
    } return k;
}

const int MAX=2000000;
int a[MAX];
//求a【ll...rr】的最长单调非增序列长度O(nlbn)
int bb[MAX];
int lcs(int *a,int ll,int rr)
{
    int i,k=1;
    bb[1]=a[ll];
    for(i=ll+1;i<=rr;i++)
    {
        if(a[i]<=bb[k]) bb[++k]=a[i];
        else
        {
            int ll=1,rr=k,mid;
            while(ll            {
                mid=(ll+rr)>>1;
                if(bb[mid]                else ll=mid+1;
            }bb[rr]=a[i];
        }      
    } return k;
}

你可能感兴趣的:(ACM_动态规划)