Subsequence(HDU3530+单调队列) 题目链接 题面 题意 思路 代码实现如下

传送门

题面

Subsequence(HDU3530+单调队列)
题目链接
题面
题意
思路
代码实现如下

题意

找到最长的一个区间,使得这个区间内的最大值减最小值在([m,k])中。

思路

我们用两个单调队列分别维护最大值和最小值,我们记作(q1)(q2)
如果(q1)的底部的值与(q2)的底部的值大于(k),则将(q1,q2)底部中下标最小的(pop)掉,并记录下来,记作(tmp),如果(q1,q2)底部的值大于等于(m)则更新答案(ans = max(ans,i-tmp))

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********
")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://Code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 2e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n, m, k;
int a[maxn];
deque<int> q1, q2;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    while(~scanf("%d%d%d", &n, &m, &k)) {
        int ans = 0;
        while(!q1.empty()) q1.pop_back();
        while(!q2.empty()) q2.pop_back();
        int tmp = 0;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            while(!q1.empty() && a[i] > a[q1.front()]) q1.pop_front();
            q1.push_front(i);
            while(!q2.empty() && a[i] < a[q2.front()]) q2.pop_front();
            q2.push_front(i);
            while(!q1.empty() && ! q2.empty() && a[q1.back()] - a[q2.back()] > k) {
                if(q1.back() > q2.back()) tmp = q2.back(), q2.pop_back();
                else tmp = q1.back(), q1.pop_back();
            }
            if(!q1.empty() && !q2.empty() && a[q1.back()] - a[q2.back()] >= m) {
                ans = max(ans, i - tmp);
            }
        }

        printf("%d
", ans);
    }
    return 0;
}