您的位置:首页 > 产品设计 > UI/UE

HDU - 3530 Subsequence 单调队列

2017-01-16 21:15 211 查看
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.
InputThere 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,1000001,100000.
m and k are in the range 0,10000000,1000000.
The second line has n integers, which are all in the range
0,10000000,1000000.

Proceed to the end of file.
OutputFor 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

题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,

m<=s<=k,求满足条件的最长子区间

            思路: 维护i位置前的最值与最小值的位置 取最远的区间值 更新ans
             用两个单调队列维护 i位置前的递增 递减 序列
ACcode:
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const int maxn = 100010;
int dpmaxqueue[maxn],dpminqueue[maxn];
int num[maxn];
int n,m,k;
int main(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
int maxtop=1,maxend=1;
int mintop=1,minend=1;
int now = 1;
int ans = 0;
for(int i=1;i<=n;i++){
while(maxtop < maxend && num[dpmaxqueue[maxend-1]] < num[i] )  maxend--;
while(mintop < minend && num[dpminqueue[minend-1]] > num[i] )  minend--;
dpmaxqueue[maxend++]=dpminqueue[minend++]=i;
while(maxtop < maxend && mintop < minend && num[dpmaxqueue[maxtop]] - num[dpminqueue[mintop]] >k){
if(dpmaxqueue[maxtop] < dpminqueue[mintop])  now =dpmaxqueue[maxtop++]+1;
else                                         now =dpminqueue[mintop++]+1;
}
if(maxtop < maxend && mintop < minend && num[dpmaxqueue[maxtop]] - num[dpminqueue[mintop]] >=m){
ans=max(ans,i-now+1);
}
}
printf("%d\n",ans);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: