• ch_5501 环路运输


    单调队列

    zhk手写单调队列定义:

    初始时  l=r=0

    (l,r]  为有效元素

    删除最左元素  ++l

    删除最右元素  --r

    队列有有效元素:l+1<=r ,即  l<r

    取最左点  q[l+1]

    取最右点  q[r]

    #include<iostream> 
    #include<cstdio>
    
    #define ri register int
    #define u int
    
    namespace opt {
        
        inline u in() {
            u x(0),f(1);
            char s(getchar());
            while(s<'0'||s>'9') {
                if(s=='-') f=-1;
                s=getchar();
            }
            while(s>='0'&&s<='9') {
                x=(x<<1)+(x<<3)+s-'0';
                s=getchar();
            }
            return x*f;
        }
        
    }
    
    using opt::in;
    
    #define NN 1000005
    
    #include<algorithm>
    
    namespace mainstay {
        
        u q[NN],a[NN],l,r,N,ans;
        
        inline void solve(){
            N=in();
            for(ri i(1);i<=N;++i) a[i]=a[i+N]=in();
            for(ri i(1);i<=(N<<1);++i){
                while(l+1<=r&&q[l+1]<i-(N>>1)) ++l;
                if(l+1<=r) ans=std::max(ans,a[q[l+1]]-q[l+1]+i+a[i]);
                while(l+1<=r&&a[q[r]]-q[r]<=a[i]-i) --r;
                q[++r]=i;
            } 
            std::cout<<ans;
        }
        
    }
    
    int main() {
        
        //freopen("x.txt","r",stdin);
        std::ios::sync_with_stdio(false);
        mainstay::solve();
        
    }
  • 相关阅读:
    IOS7 About
    iOS Newsstand Tutorial
    微信开发商
    网络流量监控相关资料
    EDM about
    thinkphp验证码的实现
    thinkphp表单上传文件并将文件路径保存到数据库中
    thinkphp分页实现
    linux 系统简单备份
    Google Hacking总结
  • 原文地址:https://www.cnblogs.com/ling-zhi/p/11815716.html
Copyright © 2020-2023  润新知