Test instructions
Give you n number and a K
Divide n into a continuous n-k+1 interval.
The first line outputs the minimum value for each interval sequentially, and the second row is the maximum value.
Ideas:
Monotone queue template problem, it is noted here that when inserting the tail of the team need to accelerate the two points
Code:
#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "#include" Cmath "using namespace std; #define N 2222222struct node{int x,s;}; int CMP (node A,node b) {return A.S<B.S;} int CMP1 (node A,node b) {return A.S>B.S;} Node Q[n],v[n];int finde_max (int l,int r,int k) {int ans=-1; while (l<=r) {int mid= (L+R)/2; if (q[mid].s>k) {ans=mid; l=mid+1; } else r=mid-1; } return ans; int finde_min (int l,int r,int k) {int ans=-1; while (l<=r) {int mid= (L+R)/2; if (q[mid].s<k) {ans=mid; l=mid+1; } else r=mid-1; } return ans; int main () {int n,k; while (scanf ("%d%d", &n,&k)!=-1) {for (int i=1; i<=n; i++) {scanf ("%d", &V[I].S); V[i].x=i; } int head=0,ed=0; for (int i=1;i<=k;i++) q[ed++]=v[i]; Sort (q,q+k,cmp); for (int i=k; i<=n; i++) {if (head>ed) q[++ed]=v[i]; Queue else {int tep=finde_min (HEAD,ED,V[I].S); if (tep==-1) q[ed=head]=v[i]; else Q[ed=tep+1]=v[i]; } while (q[head].x+k<=i) head++; Out of the team printf (i==n?) %d\n ":"%d ", Q[HEAD].S); } head=0,ed=0; for (int i=1;i<=k;i++) q[ed++]=v[i]; Sort (Q,Q+K,CMP1); for (int i=k; i<=n; i++) {if (head>ed) q[++ed]=v[i]; Queue else {int Tep=finde_max (HEAD,ED,V[I].S); if (tep==-1) q[ed=head]=v[i]; else Q[ed=tep+1]=v[i]; } while (q[head].x+k<=i) head++; Out of the team printf (i==n?) %d\n ":"%d ", Q[HEAD].S); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Monotone queue] POJ 2823 Sliding Window