hdu 3530 Subsequence 单调队列

来源:互联网 发布:云雀知止的叫是母的吗 编辑:程序博客网 时间:2024/05/22 07:39



Subsequence

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


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
 

Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3529 3528 3527 3415 3531 
 

Statistic | Submit | Discuss | Note

题意:给出一个序列,问满足最大元素值-最小元素值在给定区间[m,k]内的子串的最大长度是多少。

分析:1.由于区间范围是定的

           2.从左向右枚举右端点,相应的左端点如果越远,那么Max-Min就越大,如果越近则越小。为了将差值控制在[-∝,k]范围内,先要将上次的左端点右移一段,在第一个满足差值<=k的位置停止。

            此时一定可以保证<=k,如果满足>=m,那么更新答案。否则不必更新,也不必移动左端点,因为左端点左移必定会使之不满足差值<=k的条件,如果右移差值只会减小,同样不能够满足>=m的条件。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn=100000    ;int n,L,R;int a[maxn+10],qMax[maxn+10],qMin[maxn+10];int main(){   std::ios::sync_with_stdio(false);   while(cin>>n>>L>>R)   {       for1(i,n)  cin>>a[i];       int ans=0;       int rMax=0,fMax=0,rMin=0,fMin=0;       int le=1;       for(int i=1;i<=n;i++)       {           while(rMax-fMax>=1&& a[qMax[rMax-1] ]<=a[i]   )   rMax--;           qMax[rMax++]=i;           while(rMin-fMin>=1&& a[qMin[rMin-1] ]>=a[i]   )   rMin--;           qMin[rMin++]=i;           while(le<i&&a[qMax[fMax]]-a[qMin[fMin]]>R)           {               if(qMax[fMax]==le) fMax++;               if(qMin[fMin]==le) fMin++;               le++;           }           if( a[qMax[fMax]]-a[qMin[fMin]]>=L )  ans=max(ans,  i-le+1);       }       printf("%d\n",ans);   }   return 0;}


0 0