• 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;
    }
  • 相关阅读:
    mysql数据库
    Mysql之sql语句操作
    mysql修改root密码的多种方法
    kvm虚拟化
    清华AIOps算法:KPI聚类
    有点扯的预测方法
    内网安全运营的逻辑体系架构
    SpringBoot定时消费Kafka消息
    kafka的consumer消费能力很低的情况下的处理方案
    Kafka_Kafka 消费者 偏移量 与 积压 查询脚本 kafka-consumer-groups.sh
  • 原文地址:https://www.cnblogs.com/sweat123/p/5662371.html
Copyright © 2020-2023  润新知