双端队列LIS问题

双端队列 L I S LIS LIS问题

题目描述

在这里插入图片描述

具体做法与心路历程

考试时都想出来了结果犯了个 s b sb sb问题导致了爆零。一开始想的 d p dp dp到后来乱搞。论途中想出了正解结果被自己否定是什么感觉?

具体做法

考虑先搞出一个最长上升子序列:(图中青色部分)

KaFwan.png

那么接下来就是首先在 1 1 1前面选一截倒过来接在 1 1 1前面, 1 , 2 1,2 1,2中间选一截再搞过了接在前面,再在 3 , 4 3,4 3,4中间选一截倒过来往前接,再在 4 4 4后面选一截倒过来往前接。

设最长上升子序列的开头为 a [ i ] a[i] a[i],那么就是要选一个严格最长下降子序列且开头元素小于 a [ i ] a[i] a[i]的出来和最长上升子序列组合。

考试时傻逼,认为上升序列会与下降序列有重合!!!

求出每个点的最长上升子序列和最长下降子序列,然后枚举最长上升子序列的开头,找最大的开头为 a [ i ] a[i] a[i]小的最长下降子序列即可。

这个等价于把整个序列倒过来拼在原序列前面,再跑一边最长上升子序列即可。

两种方法都行。

C o d e \mathcal{Code} Code

/*******************************
Author:galaxy yr
LANG:C++
Created Time:2019年10月24日 星期四 15时07分30秒
*******************************/
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

struct IO{
    template<typename T>
    IO & operator>>(T&res)
    {
        T q=1;char ch;
        while((ch=getchar())<'0' or ch>'9')if(ch=='-')q=-q;
        res=(ch^48);
        while((ch=getchar())>='0' and ch<='9') res=(res<<1)+(res<<3)+(ch^48);
        res*=q;
        return *this;
    }
}cin;

const int maxn=1e5+10;
int n,a[maxn],b[2*maxn],tmp[2*maxn],*que,tot,l,r,stk[maxn],top,ans;

int lower_bound(int l,int r,int val)
{
    int ans=r+1,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(que[mid]>=val)
            ans=mid,r=mid-1;
        else
            l=mid+1;
    }
    return ans;
}

int main()
{
    //freopen("dequexlis.in","r",stdin);
    //freopen("dequexlis.out","w",stdout);
    cin>>n; tot=n;
    que=tmp+maxn;
    for(int i=1;i<=n;i++)
        cin>>a[i],b[n-(i-1)]=a[i],b[n+(i-1)]=a[i];
    l=r=1;
    que[l]=b[1];
    for(int i=2;i<=2*n-1;i++)
    {
        if(b[i]>que[r])
            que[++r]=b[i];
        else
        {
            int loc=lower_bound(l,r,b[i]);
            que[loc]=b[i];
        }
    }
    printf("%d\n",r);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值