Subsequence(HDU3530+单调队列)

题目链接

传送门

题面

1322898-20190711203402525-1242525854.png

题意

找到最长的一个区间,使得这个区间内的最大值减最小值在\([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("*********\n")
#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\n", ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/Dillonh/p/11172583.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值