[ACM] Poj 2823 Sliding Window (monotone queue)

Source: Internet
Author: User

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 36212 Accepted: 10723
Case Time Limit: 5000MS

Description

An array of size N≤106 is given to you. There is a sliding window of size kWhich is moving from the very left of the array to the very right. can only see the kNumbers in the window. Each of the sliding window moves rightwards by one position. Following is an example:
The array is[1 3-1-3 5 3 6 7], and kis 3.
Window Position Minimum Value Maximum Value
[1 3-1]-3 5 3 6 7 -1 3
1 [3-1-3] 5 3 6 7 -3 3
1 3 [-1-3 5] 3 6 7 -3 5
1 3-1 [-3 5 3] 6 7 -3 5
1 3-1-3 [5 3 6] 7 3 6
1 3-1-3 5 [3 6 7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of the lines. The first line contains integers Nand kWhich is the lengths of the array and the sliding window. There is NIntegers in the second line.

Output

There is lines in the output. The first line gives the minimum values in the windows at each position, from left to right, respectively. The second line gives the maximum values.

Sample Input

8 31 3-1-3 5 3 6 7

Sample Output

-1-3-3-3 3 33 3 5 5 6 7

Source

POJ monthly--2006.04.28, Ikki

Problem Solving Ideas:

It is not explained that the headache is cracked by the monotonous queue.

Code:

#include <iostream> #include <stdio.h>using namespace std;const int Maxn=1000005;int n,k;int q1[maxn],q2[ Maxn],num[maxn],min[maxn],max[maxn];int front1,rear1,front2,rear2,cnt1,cnt2;void in1 (int i)//queue, monotonically increment, save minimum value {while (    Front1<=rear1&&num[q1[rear1]]>num[i]) rear1--; Q1[++rear1]=i;}    void in2 (int i)//monotonically decreasing, saving maximum value {while (front2<=rear2&&num[q2[rear2]]<num[i]) rear2--; Q2[++rear2]=i;}    void out1 (int i) {if (q1[front1]<=i-k) front1++; MIN[CNT1++]=NUM[Q1[FRONT1]];}    void Out2 (int i) {if (q2[front2]<=i-k) front2++; Max[cnt2++]=num[q2[front2]];}        int main () {while (~scanf ("%d%d", &n,&k)) {front1=front2=cnt1=cnt2=0;        Rear1=rear2=-1;        for (int i=1;i<=n;i++) scanf ("%d", &num[i]);        for (int i=1;i<k;i++)//First k-1 number is only queued because it is impossible to reach the team condition {in1 (i); in2 (i);            } for (int i=k;i<=n;i++) {in1 (i); out1 (i); In2 (i); Out2 (i);        } for (int i=0;i<cnt1-1;i++) printf ("%d", min[i]);        printf ("%d\n", Min[cnt1-1]);        for (int i=0;i<cnt2-1;i++) printf ("%d", max[i]);    printf ("%d\n", max[cnt2-1]); } return 0;}


 

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.