Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3530
Test instructions
Find a longest interval where the difference between the maximum and minimum values is greater than or equal to or less than K
Analysis:
Maintaining the maximum and minimum values, and then the difference between the maximum maximum value and the minimum minimum value is not greater than Y, which is greater than y who deleted in front of the record starting point.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < queue>using namespace Std;const int maxn = 100010;int A[maxn];int main () {int n,m,k; while (~SCANF ("%d%d%d", &n,&m,&k)) {for (int i=1;i<=n;i++) scanf ("%d", &a[i]); Deque<int > Mmax; Deque<int > Mmin; int ans = 0; int st = 0; for (int i=1;i<=n;i++) {while (!mmax.empty () &&a[i]>a[mmax.back ())) mmax.pop_back ();//Maintenance monotonicity while (!mmin.empty () &&a[i]<a[mmin.back ()]) mmin.pop_back ();//Maintenance monotonicity mmax.push_back (i); Mmin.push_back (i); while (!mmax.empty () &&!mmin.empty () &&a[mmax.front ()]-a[mmin.front ()]>k) {if (Mmax.front () & Lt;mmin.front ()) {st = Mmax.front (); Mmax.pop_front (); } else {St=mmin.front (); Mmin.pop_front (); }} if (!mmax.empty () &&!mmin.empty () &&a[mmax.front ()]-a[mmin.front ()]>=m) ans = max (ans,i-st); } cout<<ans<<endl; } return 0;}
HDU 3530 subsequence (dp+ monotone queue)