luogu P3572 [POI2014]PTA-Little Bird 单调队列优化dp

#include <cstdio>
#include <iostream>
using namespace std;
int n,m,x;
int hh,tt,a[1000001],q[1000001],f[1000001];
int read()
{
    char ch=getchar();
    int res=0,w=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        res=res*10+ch-'0';
        ch=getchar();
    }
    return res*w;
}
int main()
{
    n=read();
    for(register int i=1; i<=n; i++) 
        a[i]=read();
    m=read();
    while(m--)
    {
        x=read();
        hh=tt=1;
        q[tt++]=1;
        for(register int i=2; i<=n; i++)
        {
            while(hh<tt&&i-q[hh]>x)
                hh++;
            if(a[q[hh]]>a[i])
                f[i]=f[q[hh]];
            else
                f[i]=f[q[hh]]+1;
            //                                                        维护递减的 
            while(hh<=tt&&(f[q[tt-1]]>f[i]||(f[q[tt-1]]==f[i]&&a[q[tt-1]]<=a[i])))
                tt--;
            q[tt++]=i;
        }
        printf("%d
",f[n]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/13036916.html