POJ2823 Sliding Window "Double-ended queue"

Source: Internet
Author: User

The maximum minimum value of k is sliding, sliding one at a time

Maintaining possible answer values with a double-ended queue

Maintain a monotonically increasing sequence if minimum values are required

To the beginning of the first K, the newly added if smaller than the end of the team, then the tail of the team, until the new join the team tail, join the tail

From K+1 to the last one, press in the new number according to the above rules, and then eject the first element of the team (the position that satisfies the original sequence of the first element of the team must be in the window, otherwise, continue to pop the next)

#include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <        Cstring> #include <cmath>using namespace std;inline void put (int x) {if (x< 0) {putchar ('-');    x =-X;        } if (x = = 0) {Putchar (' 0 ');    Return    } Char s[20];    int bas = 0;    for (; x;x/=10) s[bas++] = x%10+ ' 0 ';    for (; bas--;) Putchar (S[bas]); return;} int n,k;int num[1111111];int ansmin[1111111];int pj;int ansmax[1111111];int pjj;struct node{int p,v;} Q[1111111];int Main () {#ifndef online_judgefreopen ("G:/1.txt", "R", stdin); Freopen ("G:/2.txt", "w", stdout); # ENDIFSCANF ("%d%d", &n,&k), for (int i=1;i<=n;i++) {scanf ("%d", &num[i]);}            int start=1,tail=1;for (int i=1;i<=n;i++) {if (i==1) {q[1].v=num[1];        Q[1].p=1; }//for (int j=tail;j>=start;j--)//eject larger number than it//{//if (Q[j].v>num[i])//tail--;//}while (tail>=start&&q [Tail].v>num[i]) tail--;q[++tail].v=num[i];q[tail].p=i;Add this number to the tail//Start output minimum if (i>=k) {while (i-q[start].p>k-1) start++;ansmin[pj++]=q[start].v;}}            start=1;tail=1;for (int i=1;i<=n;i++) {if (i==1) {q[1].v=num[1];        Q[1].p=1; }//for (int j=tail;j>=start;j--)//eject a number smaller than it//{//if (Q[j].v<num[i])//tail--;//}while (tail>=start&&q [Tail].v<num[i]) tail--;q[++tail].v=num[i];q[tail].p=i;//adds the number to the tail//Start output minimum if (i>=k) {while (i-q[start].p>k -1) start++;ansmax[pjj++]=q[start].v;}} for (int i=0;i<pj;i++) {put (ansmin[i]);//printf ("%d%c", ansmin[i],i==pj-1? ' \ n ': ');p Utchar (');} Putchar (' \ n '); for (int i=0;i<pjj;i++) {put (ansmax[i]);//printf ("%d%c", ansmax[i],i==pjj-1? ' \ n ': ');p Utchar (');} Putchar (' \ n ');}


 

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.