zoukankan      html  css  js  c++  java
  • hdu3530 单调队列

    Subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5809    Accepted Submission(s): 1911


    Problem Description
    There is a sequence of integers. Your task is to find the longest subsequence that satisfies the following condition: the difference between the maximum element and the minimum element of the subsequence is no smaller than m and no larger than k.
     

    Input
    There are multiple test cases.
    For each test case, the first line has three integers, n, m and k. n is the length of the sequence and is in the range [1, 100000]. m and k are in the range [0, 1000000]. The second line has n integers, which are all in the range [0, 1000000].
    Proceed to the end of file.
     

    Output
    For each test case, print the length of the subsequence on a single line.
     

    Sample Input
    
    
    5 0 0 1 1 1 1 1 5 0 3 1 2 3 4 5
     

    Sample Output
    
    
    5 4
     

    Source
     
     
    题意:
    求n个数里面最长的一段,其最大值减最小值的差>=m && <= k。
    思路:
    维护2个单调队列。一个存最大值,一个存最小值。
     
    /*
     * Author:  sweat123
     * Created Time:  2016/7/12 9:09:45
     * File Name: main.cpp
     */
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<time.h>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1<<30
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define key_value ch[ch[root][1]][0]
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 100010;
    int a[MAXN];
    deque<int>q1,q2;
    int n,m,k;
    int main(){
        while(~scanf("%d%d%d",&n,&m,&k)){
            q1.clear();
            q2.clear();
            for(int i = 1; i <= n; i++){
                scanf("%d",&a[i]);
            }   
            int ans = 0;
            int bf = 0;
            for(int i = 1; i <= n; i++){
                while(!q1.empty() && a[q1.back()] < a[i]){
                    q1.pop_back();
                }   
                while(!q2.empty() && a[q2.back()] > a[i]){
                    q2.pop_back();   
                }
                q1.push_back(i);
                q2.push_back(i);
                while(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] > k){
                    if(q1.front() < q2.front()){
                        bf = q1.front();
                        q1.pop_front();
                    } else if(q1.front() > q2.front()){
                        bf = q2.front();
                        q2.pop_front();   
                    } else {
                        bf = q1.front();
                        q1.pop_front();
                        q2.pop_front();   
                    }
                }
                if(!q1.empty() && !q2.empty() && a[q1.front()] - a[q2.front()] >= m){
                    ans = max(ans,i - bf);
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    TransactionScop事务机制的使用
    MVC无刷新上传图片并显示
    WebClient和WebRequest获取html代码
    Web.config配置详解
    分类和扩展有什么区别?可以分别用来做什么?分类有哪些局限性?分类的结构体里面有哪些成员?
    有序二维数组的查找
    生成Excel.xlsx文件 iOS
    charles Https抓包
    https 通信流程和Charles 抓包原理
    fastlane自动化打包ipa并发布到firim或者蒲公英
  • 原文地址:https://www.cnblogs.com/sweat123/p/5662371.html
Copyright © 2011-2022 走看看