首页 > 代码库 > POJ 2823 Sliding Window

POJ 2823 Sliding Window

Sliding Window
 

Description

  An array of size n ≤ 106 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 positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

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 31 3 -1 -3 5 3 6 7

Sample Output

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

题意:
  一个长度为n的序列,用长度为看k的窗口在上面移动,求窗口中数字最大为多少。
分析:
  单调队列求,入门题目收下了。、

技术分享
 1 # include <iostream> 2 using namespace std; 3  4 const int MAX = 1000010; 5 int a[MAX]; 6 int q[MAX]; 7 int p[MAX]; 8 int Min[MAX]; 9 int Max[MAX];10 11 int n, k;12 13 void get_Min()14 {15     int head = 1, tail = 0;16     for(int i = 0; i < k - 1; i++)17     {18         while(head <= tail && q[tail] >= a[i])19             tail--;20         tail++;21         q[tail] = a[i];22         p[tail] = i;23     }24     for(int i = k - 1; i < n; i++)25     {26         while(head <= tail && q[tail] >= a[i])27             tail--;28         tail++;29         q[tail] = a[i];30         p[tail] = i;31         while(p[head] < i - k + 1)32         {33             head++;34         }35         Min[i - k + 1] = q[head];36     }37 }38 void get_Max()39 {40     int head = 1, tail = 0;41     for(int i = 0; i < k - 1; i++)42     {43         while(head <= tail && q[tail] <= a[i])44             tail--;45         tail++;46         q[tail] = a[i];47         p[tail] = i;48     }49     for(int i = k - 1; i < n; i++)50     {51         while(head <= tail && q[tail] <= a[i])52             tail--;53         tail++;54         q[tail] = a[i];55         p[tail] = i;56         while(p[head] < i - k + 1)57         {58             head++;59         }60         Max[i - k + 1] = q[head];61     }62 }63 64 65 66 int main()67 {68     scanf("%d%d", &n, &k);69     for(int i = 0; i < n; i++)70         scanf("%d", &a[i]);71     get_Min();72     get_Max();73     for(int i = 0; i < n - k + 1; i++)74     {75         if(i == 0)76             printf("%d", Min[i]);77         else78             printf(" %d", Min[i]);79     }80     printf("\n");81     for(int i = 0; i < n - k + 1; i++)82     {83         if(i == 0)84             printf("%d", Max[i]);85         else86             printf(" %d", Max[i]);87     }88     printf("\n");89     return 0;90 }
View Code

 


POJ 2823 Sliding Window