POJ 2823 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 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 QAQ Query_Max ( int q , int w , int i ) 2 { 3 if(q <= tr[i].l && w >= tr[i].r )return tr[i].maxtr ; 4 else 5 { 6 QAQ mid = (tr[i].l + tr[i].r ) >> 1; 7 if(q > mid) 8 { 9 return Query_Max ( q , w , i << 1 | 1); 10 } 11 else if(w <= mid) 12 { 13 return Query_Max ( q , w , i << 1); 14 } 15 else 16 { 17 return Max( Query_Max ( q , w , i << 1) , Query_Max ( q , w , i << 1 | 1)); 18 } 19 } 20 } 21 22 23 QAQ Query_Min ( int q , int w , int i ) 24 { 25 if(q <= tr[i].l && w >= tr[i].r )return tr[i].mintr ; 26 else 27 { 28 QAQ mid = (tr[i].l + tr[i].r ) >> 1; 29 if(q > mid) 30 { 31 return Query_Min ( q , w , i << 1 | 1); 32 } 33 else if(w <= mid) 34 { 35 return Query_Min ( q , w , i << 1); 36 } 37 else 38 { 39 return Min( Query_Min ( q , w , i << 1) , Query_Min ( q , w , i << 1 | 1)); 40 } 41 } 42 }
注:这里QAQ就是long long 用typedef long long QAQ;定义的。
建树及Push_up操作:
1 void Push_up (int i) 2 { 3 tr[i].maxtr = Max ( tr[i << 1].maxtr , tr[i << 1 | 1].maxtr); 4 tr[i].mintr = Min ( tr[i << 1].mintr , tr[i << 1 | 1].mintr); 5 } 6 7 void Build_Tree (int x , int y , int i) 8 { 9 tr[i].l = x ; 10 tr[i].r = y ; 11 if( x == y )tr[i].maxtr = tr[i].mintr = arr[x] ; 12 else 13 { 14 QAQ mid = (tr[i].l + tr[i].r ) >> 1 ; 15 Build_Tree ( x , mid , i << 1 ); 16 Build_Tree ( mid + 1 , y , i << 1 | 1); 17 Push_up ( i ); 18 } 19 }
以上就是用线段树解法,是线段树的简单应用,本题还有很多其他写法,比如维护单调队列,比线段树更容易实现代码并且代码量较少,以下是维护单调队列的代码:
1 #include "stdio.h" 2 #define maxn (1000100) 3 int n, K; 4 int Head, Tail; 5 int val[maxn]; 6 int numb[maxn]; 7 bool Flag; 8 inline bool cmp(int a, int b) 9 { 10 return Flag ? a < b : a > b; 11 } 12 void Push(int idx) 13 { 14 while(Head < Tail && cmp(val[idx], val[numb[Tail - 1]])) Tail --; 15 numb[Tail++] = idx; 16 while(Head < Tail && idx - numb[Head] + 1 > K) Head ++; 17 } 18 int main() 19 { 20 scanf("%d %d", &n, &K); 21 for(int i = 1; i <= n; i++) scanf("%d", &val[i]); 22 Head = 0 , Tail = 0 , Flag = true; 23 for(int i = 1; i < K; i++) Push(i); 24 for(int i = K; i <= n; i++) 25 { 26 Push(i); 27 printf("%d ", val[numb[Head]]); 28 } 29 puts(""); 30 Head = 0 , Tail = 0 , Flag = false; 31 for(int i = 1; i < K; i++) Push(i); 32 for(int i = K; i <= n; i++) 33 { 34 Push(i); 35 printf("%d ", val[numb[Head]]); 36 } 37 puts(""); 38 return 0; 39 }
(完)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·~~~~~~~~~~~~~~