Question: Given a sequence of length n, the difference between the maximum and minimum values in the interval of the length of M is less than or equal to C
Using a monotone queue to maintain the maximum and minimum values in the interval--hard---
It's cool to brush water--
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001using namespace Std;int n,m,c,a[m];int q_max[m],r_max,h_max;int q_min[m],r_min,h_min;bool flag;int main () {int I ; Cin>>n>>m>>c;for (i=1;i<=n;i++) {scanf ("%d", &a[i]); { int &r=r_max,&h=h_max,*q=q_max;while (r-h>=1 && a[q[r]]<a[i]) q[r--]=0;q[++r]=i;while (i-q[h+1 ]>=m) q[++h]=0;} {int &r=r_min,&h=h_min,*q=q_min;while (r-h>=1 && a[q[r]]>a[i]) q[r--]=0;q[++r]=i;while (i-q[h+ 1]>=m) q[++h]=0;} if (i>=m && a[q_max[h_max+1]]-a[q_min[h_min+1]]<=c) printf ("%d\n", i-m+1), flag=1;} if (!flag) puts ("NONE"); return 0;}
Bzoj 1342 Baltic2007 Sound mute problem monotone queue