HDU-3530 Subsequence(单调队列)
Posted 可惜没如果=_=
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-3530 Subsequence(单调队列)相关的知识,希望对你有一定的参考价值。
Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7390 Accepted Submission(s): 2498
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
有一点突破的迹象qwq 就是求某一个区间的最值并且这个区间的左右端点在变的情况一般都用单调队列 or 单调栈 来做QwQ
dalao勿喷qwq
1 #include "bits/stdc++.h" 2 using namespace std; 3 typedef long long LL; 4 const int MAX=1e5+5; 5 int n,m,k; 6 int a[MAX]; 7 deque <int> q1,q2; 8 inline int read(){ 9 int an=0,x=1;char c=getchar(); 10 while (c<‘0‘ || c>‘9‘) {if (c==‘-‘) x=-1;c=getchar();} 11 while (c>=‘0‘ && c<=‘9‘) {an=an*10+c-‘0‘;c=getchar();} 12 return an*x; 13 } 14 int main(){ 15 freopen ("subsequence.in","r",stdin); 16 freopen ("subsequence.out","w",stdout); 17 int i,j; 18 while (~scanf("%d%d%d",&n,&m,&k)){ 19 int ans=0,last=0; 20 for (i=1;i<=n;i++) a[i]=read(); 21 while (q1.size()) q1.pop_back(); while (q2.size()) q2.pop_back(); 22 for (i=1;i<=n;i++){ 23 while (q1.size() && a[i]>a[q1.back()]) q1.pop_back(); 24 while (q2.size() && a[i]<a[q2.back()]) q2.pop_back(); 25 q1.push_back(i),q2.push_back(i); 26 while (q1.size() && q2.size() && (a[q1.front()]-a[q2.front()])>k){ 27 if (q1.front()<q2.front()){ 28 last=q1.front(),q1.pop_front(); 29 } 30 else if (q1.front()>q2.front()){ 31 last=q2.front(),q2.pop_front(); 32 } 33 else{ 34 last=q1.front(),q1.pop_front(),q2.pop_front(); 35 } 36 } 37 if (q1.size() && q2.size() && (a[q1.front()]-a[q2.front()])>=m){ 38 ans=max(ans,i-last); 39 } 40 } 41 printf("%d\n",ans); 42 } 43 return 0; 44 }
以上是关于HDU-3530 Subsequence(单调队列)的主要内容,如果未能解决你的问题,请参考以下文章