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
Sample Output
5
4
题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,
m<=s<=k,求满足条件的最长子区间
思路: 维护i位置前的最值与最小值的位置 取最远的区间值 更新ans
用两个单调队列维护 i位置前的递增 递减 序列
ACcode:
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; }
相关文章推荐
- hdu 3474 Necklace 单调队列 DP
- HDU 4193 Non-negative Partial Sums(单调队列)
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
- HDU 4374 One hundred layer(单调队列+DP)
- hdu 4374 dp+单调队列优化
- hdu 3401 Trade //单调队列+DP
- hdu 3415 Max Sum of Max-K-sub-sequence(单调队列)
- hdu Max Sum of Max-K-sub-sequence 单调队列优化DP
- 单调队列的一个应用——求解连续区间最大值(HDU Max Sum of Max-K-sub-sequence)
- hdu 3415 单调队列1
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
- hdu 1506#单调队列
- hdu 4193 Non-negative Partial Sums 单调队列
- hdu 3530 Subsequence 单调队列 求最长连续区间,满足条件(m<=max-min<=k)
- hdu 2191 单调队列优化
- hdu 3415 Max Sum of Max-K-sub-sequence【单调队列】
- HDU 1003——Max Sum (单调队列做法)
- hdu Max Sum of Max-K-sub-sequence(单调队列)
- HDU 4374 单调队列优化 DP
- hdu 3415 Max Sum of Max-K-sub-sequence 单调队列 求连续l(1<=l<=k)个数的和的最大值 数列可循环