zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    csv导入数据到mongodb3.2
    [转]教你十分钟下载并破解IntelliJ IDEA(2017)
    Richard Stallman:让我们关注和尊敬自由软件教父
    约翰·卡马克和他的id Software
    fvwm:还是觉得你最好
    《程序员修炼之道》读书心得
    平铺式窗口管理器 Musca 初体验
    最小主义:我的Musca桌面环境
    Vim,Emacs排名不分先后
    在Emacs中画思维导图
  • 原文地址:https://www.cnblogs.com/Dillonh/p/11172583.html
Copyright © 2011-2022 走看看