Sliding window--poj2823 of acm-monotone queue

Source: Internet
Author: User

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 36326 Accepted: 10762
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
Title: http://poj.org/problem?id=2823
HDU Magic Board This problem, someone mentioned the O (1) queue, so the degree Niang search to this problem. Do it for a moment ~
① What is an O (1) queue?
O (1) The queue is also called the monotone queue, as the name implies, the elements in the queue is monotonous.
Either monotonically increasing, or monotonically decreasing.
So what's the use of building a monotonous queue?
After we maintain a queue, we find that the maximum/minimum value of the element in the current queue can be the  speed of O (1).
Maybe someone (like I just started thinking about it) Q: With priority queue, set the priority level is not possible.
However, to know the STL, it is time-consuming, especially the priority queue.
Generally use the monotonous queue to do the problem, the time requirements are relatively high, so the priority queue obviously can not meet the requirements.
② How monotonic queues are maintained
Example of a monotonically increasing queue:

1, if the length of the queue is certain, first determine whether the first element of the team within the specified range, if the super-range growth team first.

2, each time adding elements and team tail comparison, if the current element is less than the tail and the queue is not empty, then reduce the tail pointer, the team tail elements in turn, until the queue to meet the adjustment of the

Pay special attention to the application of the head pointer and the tail pointer.

Reference: http://www.cnblogs.com/neverforget/archive/2011/10/13/ll.html

http://xuyemin520.is-programmer.com/posts/25964

And this problem can be solved.

/****************************************************************************** Author:Tree **From : Http://blog.csdn.net/lttree * * title:sliding Window **source:poj 2823 * * Hint: Monotone Queue ******************************************************************************/#include <stdio.h    > #define MAX 1000001int n,k;int pre1,pre2,lst1,lst2,len_max,len_min;      Two queue head pointer with tail pointer, maximum subscript, minimum value of the following table int Num[max],increase[max],decrease[max],max[max],min[max]; Num saves the data, incrementing the array with the descending queue, maximum value, and minimum value.    The indentation operation of the increment sequence is void In_max (int i) {while (Pre1<=lst1 && num[Increase[lst1]]<num[i])--lst1;    Increase[++lst1]=i;        If the number is greater than or equal to K, you need to assign a value to the maximum array if (I&GT;=K) {if (increase[pre1]<=i-k) pre1++;    max[len_max++]=num[Increase[pre1]];    }}//indentation of descending sequence void in_min (int i) {while (Pre2<=lst2 && num[Decrease[lst2]]>num[i])--lst2;    Decrease[++lst2]=i; // If the number is greater than or equal to K, you need to assign a value to the minimum array if (i>=k) {if (decrease[pre2]<=i-k) pre2++;    min[len_min++]=num[Decrease[pre2]];    }}int Main () {int i;        while (~SCANF ("%d%d", &n,&k)) {//Initialize pre1=pre2=len_max=len_min=0;        Lst1=lst2=-1;            Read-in data for (i=1;i<=n;++i) {scanf ("%d", &num[i]);            In_max (i);        In_min (i);        }//Output data for (i=0;i<len_min-1;++i) printf ("%d", min[i]);        printf ("%d\n", min[len_min-1]);        for (i=0;i<len_max-1;++i) printf ("%d", max[i]);    printf ("%d\n", max[len_max-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.