Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3634 Accepted Submission(s): 1181
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.
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
Recommend
zhengfeng
本题要求这样的一个最长连续子串,满足m<=Max-Min<=k。
本题一个容易想到的方法就是暴力枚举,依次扩大区间维护区间最大值与最小值,更新满足条件的len,但此方法的时间复杂度为O(N^2),在n=100000时肯定超时,此法不通。
刚刚学习了单调队列,此题可以用单调队列降低时间复杂度。
以下引自《用单调性优化动态规划》(未读完)
什么是单调(双端)队列
单调队列,顾名思义,就是一个元素单调的队列,那么就能保证队首的元素是最小(最大)的,从而满足动态规划的最优性问题的需求。
单调队列,又名双端队列。双端队列,就是说它不同于一般的队列只能在队首删除、队尾插入,它能够在队首、队尾同时进行删除。
本题可以维护两个单调队列,一个单增,一个单减,前者对头即为最小值,后者对头则为最大值,循环n-1次插入所给序列。对于序列中每个具体元素,它分别入两个单调队列,前者存放比它大(包括自己)的元素,后者存放比它小(包括自己)的元素,不多不少正好包含某个连续区间的所有元素一次(自己除外);插入该元素后就的判断是否当前区间满足m<=Max-Min<=k,若Max-Min>k,区间左端点得向后滑动一个长度;否则继续判断Max-Min>=m,若满足,更新区间长度…这样一来,总的时间复杂度降到了O(N*log(N))。
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN=100000+10;
int da[MAXN],Inc[MAXN],Dec[MAXN];
int n,m,k,front1,rear1,front2,rear2;
int Queue()
{
front1=0,rear1=-1,front2=0,rear2=-1;
int i,ans=0,start=0;
for(i=0;i<n;i++)
{
while(front1<=rear1&&da[Dec[rear1]]<=da[i])//保证Dec队列在start-i区间内的递减
rear1--;
Dec[++rear1]=i;
while(front2<=rear2&&da[Inc[rear2]]>=da[i])//保证inc队列在start-i区间内的递增
rear2--;
Inc[++rear2]=i;
while(da[Dec[front1]]-da[Inc[front2]]>k)
{
//保证区间左端点向后滑动一个长度
if(Dec[front1]-Inc[front2]<0)
{
start=Dec[front1]+1;
front1++;
}
else
{
start=Inc[front2]+1;
front2++;
}
}
//满足m<=Max-Min<=k
if(da[Dec[front1]]-da[Inc[front2]]>=m)
{
if(i-start+1>ans)
ans=i-start+1;
}
}
return ans;
}
int main()
{
int i,ans;
while(~scanf("%d%d%d",&n,&m,&k))
{
for(i=0;i<n;i++)
scanf("%d",&da[i]);
ans=Queue();
printf("%d\n",ans);
}
return 0;
}