[Monotone queue] POJ 2823 Sliding Window

Source: Internet
Author: User

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&LT;B.S;} int CMP1 (node A,node b) {return A.S&GT;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", &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.