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;
}

 

转载于:https://www.cnblogs.com/beiyuoi/p/6216134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值