传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2096

【题解】

单调队列。维护上升&下降

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 3e6 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int K, n;
int a[M];
int q1[M], q2[M];
int h1, t1, h2, t2, ans;
// inc, dec 

int main() {
    h1 = h2 = 1, t1 = t2 = 0;
    int t = 1;
    cin >> K >> n;
    for (int i=1; i<=n; ++i) scanf("%d", a+i);
    for (int i=1; i<=n; ++i) {
        while(h1 <= t1 && a[q1[t1]] <= a[i]) --t1;
        while(h2 <= t2 && a[q2[t2]] >= a[i]) --t2;
        q1[++t1] = i; q2[++t2] = i;
        while(a[q1[h1]] - a[q2[h2]] > K) 
            if(q1[h1] < q2[h2]) t = q1[h1]+1, ++h1;
            else t = q2[h2]+1, ++h2;
        ans = max(ans, i-t+1);
    }
    printf("%d\n", ans);
    return 0;
}
View Code

相关文章:

  • 2021-07-20
  • 2021-09-29
  • 2021-09-29
  • 2021-07-14
  • 2021-08-27
  • 2022-12-23
  • 2021-09-25
  • 2022-02-25
猜你喜欢
  • 2021-08-03
  • 2021-12-31
  • 2022-12-23
  • 2021-05-30
  • 2021-12-16
  • 2021-12-20
  • 2022-02-25
相关资源
相似解决方案