【POI 2010】 Pilots

【题目链接】

           点击打开链接

【算法】

          单调队列

【代码】

            

#include<bits/stdc++.h>
using namespace std;
#define MAXN 3000010

int n,k,i,ans,pos;
deque< int > q1,q2;
int a[MAXN];

int main() {
        
        scanf("%d%d",&k,&n);
        for (i = 1; i <= n; i++) scanf("%d",&a[i]);
        for (i = 1; i <= n; i++)
        {
                while ((!q1.empty()) && (a[q1.back()] <= a[i])) q1.pop_back();
                q1.push_back(i);
                while ((!q2.empty()) && (a[q2.back()] >= a[i])) q2.pop_back();
                q2.push_back(i);
                while (a[q1.front()] - a[q2.front()] > k)
                {
                        if (q1.front() < q2.front())
                        {
                                pos = q1.front();
                                q1.pop_front(); 
                        } else
                        {
                                pos = q2.front();
                                q2.pop_front();
                        }
                }
                ans = max(ans,i-pos);
        }
        printf("%d
",ans);
        
        return 0;
    
}
原文地址:https://www.cnblogs.com/evenbao/p/9196290.html