HDU 3530 Subsequence(单调队列)

来源:互联网 发布:八字预测软件下载 编辑:程序博客网 时间:2024/06/05 11:42

Subsequence

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


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 01 1 1 1 15 0 31 2 3 4 5
 

Sample Output
54
 

Source
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU 
 
题意:
找出最大的区间长度,这个区间里的最大值和最小值的差值不能小于m,不能大于k。

POINT:
维护两个单调队列。当差值大于k时,从最大值和最小值中删去靠前的那一个。

#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>using namespace std;const int N = 100100+4;int a[N];deque<int> q1;int ans;deque<int> q2;int main(){    int n,m,k;    while(~scanf("%d %d %d",&n,&m,&k))    {        int ans=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        int last=0;//注意初值。        while(!q1.empty()) q1.pop_front();        while(!q2.empty()) q2.pop_front();        for(int i=1;i<=n;i++)        {            while(!q1.empty()&&a[i]>=a[q1.back()]) q1.pop_back();            while(!q2.empty()&&a[i]<=a[q2.back()]) 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())                {                    last=q2.front();                    q2.pop_front();                }                else                {                    last=q1.front();                    q1.pop_front();                }            }            if(!q1.empty()&&!q2.empty()&&a[q1.front()]-a[q2.front()]>=m)            {                ans=max(ans,i-last);            }        }        printf("%d\n",ans);    }}



原创粉丝点击