POJ2823 sliding window

Sliding minimum (maximum) value, template question.

The meaning of the question: Given a sequence and a window size k, sequentially find the maximum and minimum values ​​in each window.

It's exactly the same as the example in the challenge, just add one more to find the maximum, and change the greater than or less than symbol.

The algorithm is to use a deque:

Taking the minimum value as an example, maintain such a queue:

1. The elements in the queue store the subscript of the sequence, the elements in the sequence (subscript) are incremented, and the element in the subscript corresponding to the sequence (the corresponding value of the subscript) is also incremented.

Obviously, we start traversing i from 0 to ensure that the subscripts stored in the queue are increasing, we only need to design the algorithm to ensure that the elements in the corresponding array of subscripts are also increasing.

2. When adding a subscript, delete all subscripts whose corresponding value is greater than the corresponding value of the current subscript from the back to the front, so that the element corresponding to the subscript can also be incremented.

3. Add this subscript to the queue

4. If the first element of the queue is no longer needed later, the first element of the queue is moved back one place.

Example

n=5

k=3

a={1,3,5,4,2}

 

join 0 -> {0}

join 1 -> {0,1}

join 2 -> {0,1,2}

Current window minimum value=a0=1

delete 0 -> {1,2}

 

Add 3 -> {1,3} (a2>=a3, delete 2)

Current window minimum value=a1=3

delete 1 -> {3}

 

add 4 -> {4} (a3>=a4, delete 3)

Current window minimum value=a4=2

 

Description

An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7] , and k is 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 two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.

Output

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

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7
 1 #include<iostream>
 2 #include <cstdio>
 3 #define MAX_N 1000000 + 16
 4 
 5 using namespace std;
 6 int x[MAX_N];
 7 int b[MAX_N],c[MAX_N];
 8 int que[MAX_N];
 9 int l,r;
10 int main(int argc, char *argv[])
11 {
12     int n,k;
13     cin >> n >> k;
14     for(int i=0; i<n; i++)
15         cin >> x[i];
16     l=r=0;
17     for(int i=0; i<n; i++)
18     {
19         while(l<r&&x[que[r-1]]>=x[i] )r--;
20         que[r++]=i;
21 
22         if(i-k+1>=0)
23         {
24             b[i-k+1]=x[que[l]];
25 
26             if(que[l]==i-k+1)
27             {
28                 l++;
29             }
30         }
31     }
32     l=r=0;
33     for(int i=0; i<n; i++)
34     {
35         while(l<r&&x[que[r-1]]<=x[i] )r--;
36         que[r++]=i;
37 
38         if(i-k+1>=0)
39         {
40             c[i-k+1]=x[que[l]];
41 
42             if(que[l]==i-k+1)
43             {
44                 l++;
45             }
46         }
47     }
48     for(int i=0; i<=n-k; i++)
49         printf("%d%c",b[i],i==n-k?'\n':' ');
50     for(int i=0; i<=n-k; i++)
51         printf("%d%c",c[i],i==n-k?'\n':' ');
52     return 0;
53 }
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325235095&siteId=291194637