zoukankan      html  css  js  c++  java
  • BZOJ 2096: [Poi2010]Pilots

    Description

    求一个最长的序列,最大值最小值之差不超过 (k) .

    Sol

    单调队列.

    一个队列直接上就行..

    Code

    /**************************************************************
        Problem: 2096
        User: BeiYu
        Language: C++
        Result: Accepted
        Time:4024 ms
        Memory:71604 kb
    ****************************************************************/
     
    #include <bits/stdc++.h>
    using namespace std;
     
    #define debug(a) cout<<#a<<"="<<a<<" "
     
    typedef long long LL;
    const int N = 3e6+50;
     
    LL n,k,ans;
    LL a[N];
    LL q[2][N],h[2],t[2],l;
     
    inline LL in(LL x=0,char ch=getchar()) { while(ch>'9' || ch<'0') ch=getchar();
        while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
    int main() {
        k=in(),n=in();
        for(int i=1;i<=n;i++) a[i]=in();
         
        ans=1;
        q[0][h[0]=t[0]=1]=1,q[1][h[1]=t[1]=1]=1,l=1;
         
        for(int i=2;i<=n;i++) {
            while(h[0]<=t[0] && a[i]<a[q[0][t[0]]]) t[0]--;
            q[0][++t[0]]=i;
             
    //      cout<<i<<":"<<endl;
    //      for(int j=h[0];j<=t[0];j++) cout<<q[0][j]<<endl;
             
            while(h[1]<=t[1] && a[i]>a[q[1][t[1]]]) t[1]--;
            q[1][++t[1]]=i;
             
    //      for(int j=h[1];j<=t[1];j++) cout<<q[1][j]<<endl;
             
            for(;a[q[1][h[1]]]-a[q[0][h[0]]]>k;l++) {
                if(q[0][h[0]]==l) h[0]++;
                if(q[1][h[1]]==l) h[1]++;
            }
            ans=max(ans,i-l+1);
    //      debug(l),debug(ans)<<endl;
    //      cout<<"--------------------"<<endl;
        }cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    2017年期末获奖名单
    2018-01-17作业
    3.2.4 条件表达式
    3.2.3if语句的嵌套2
    if嵌套语句--作业题
    软工第四次作业
    软工第五次作业-结对
    软工第三次作业
    软工第二次作业——数独
    软工实践2017年第一次作业
  • 原文地址:https://www.cnblogs.com/beiyuoi/p/6216134.html
Copyright © 2011-2022 走看看