"Sliding window" monotone queue

Source: Internet
Author: User

Title Description

Give you an array of length N, a sliding form of k that moves from the leftmost to the far right, and you can see only the K-integers of the window, each time the form moves one bit to the right, as in the following table:

Your task is to find the maximum and minimum values for the window at each location.

Input Format

The 1th line of input is two integers n,k, and the 2nd behaves as an array of n (that is, there are n integers).

output Format

The output is 2 rows, the 1th line is the minimum value for each position, and the 2nd line is the maximum value for each position.

Sample Data 1enter [Copy]
8 3
1 3-1-3 5 3 6 7
Output
-1-3 -3-3 3 3
3 3 5 5 6 7
Notes

"Data Range"

For 20% of data:n<=500;
For 50% of data:n<=100000;
For 100% of data:n<=1000000;

Problem Analysis

Entry-level monotone queue. Maintain a monotonically increasing and monotonically decreasing queue, adding new numbers each time and maintaining monotonicity, then deleting the number of teams that are out of the range, and the last team is the answer.

Code
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intN =1000005, oo =0x3f3f3f3f; typedef pair<int,int>P;intData[n], head, tail, N, K; P Que[n];inlineintRead () {inti =0, F =1;CharCH =GetChar ();  for(; (Ch <'0'|| CH >'9') && ch! ='-'; CH =GetChar ()); if(ch = ='-') F =-1, ch =GetChar ();  for(; Ch >='0'&& CH <='9'; CH =GetChar ()) I= (I <<3) + (I <<1) + (CH-'0'); returnIF;} Inlinevoidwrintx) {    if(X <0) Putchar ('-'), x =-x; if(X >9) WR (X/Ten); Putchar (x%Ten+'0');}intMain () {n= Read (), k =read ();  for(inti =1; I <= N; i++) Data[i] =read (); Tail= Head =1; Que[head].second=Oo;  for(inti =1; I <= N; i++){        if(Data[i] <que[head].second) Que[head= Tail =1] =P (i, data[i]); Else{             while(Head < tail && que[tail].second > data[i]) tail--; que[++tail] =P (i, data[i]); }         while(Que[head].first <= i-k) head++; if(I >=k) WR (Que[head].second), Putchar (' ');//cout<<endl<< "!"; for (int j = head; J <= Tail; j + +) cout<< "(" <<que[j].first<< ")" <<que[j].second<< ""; cout<<endl;} putchar ('\ n'); Tail= Head =1; Que[head].second= -Oo;  for(inti =1; I <= N; i++){        if(Data[i] >que[head].second) Que[head= Tail =1] =P (i, data[i]); Else{             while(Head < tail && Que[tail].second < data[i]) tail--; que[++tail] =P (i, data[i]); }         while(Que[head].first <= i-k) head++; if(I >=k) WR (Que[head].second), Putchar (' '); //cout<<endl<< "!"; for (int j = head; J <= Tail; j + +) cout<<que[j].first<< "" <<que[j].second<< "";cout<< Endl;    }    return 0;}

"Sliding window" monotone queue

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.