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.
题意:
给一个长度为n的序列,从中找到一个最长的连续的子序列满足
最大的元素和最小的元素的差>=m&&<=k
思路:
维护两个单调队列,维护的时候是维护最大值减去最小值<=ki就行了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long LL;
const int maxn = 100005;
const int inf=(1<<28)-1;
deque<pair<int,int> >QueMax,QueMin;
int main()
{
int n,k,m;
while(~scanf("%d%d%d",&n,&m,&k))
{
int ans=0;
QueMax.clear();
QueMin.clear();
int MinPos=-1;
for(int i=0;i<n;++i)
{
int x;
scanf("%d",&x);
while(!QueMax.empty()&&QueMax.back().first<x) QueMax.pop_back();
while(!QueMin.empty()&&QueMin.back().first>x) QueMin.pop_back();
QueMax.push_back(make_pair(x,i));
QueMin.push_back(make_pair(x,i));
while(!QueMax.empty()&&!QueMin.empty()&&QueMax.front().first-QueMin.front().first>k)
{
if(QueMax.front().second>QueMin.front().second)
{
MinPos=QueMin.front().second;
QueMin.pop_front();
}
else
{
MinPos=QueMax.front().second;
QueMax.pop_front();
}
}
if(QueMax.front().first-QueMin.front().first>=m)
ans=max(ans,i-MinPos);
}
printf("%d\n",ans);
}
return 0;
}