[POJ-2823] -Sliding Window

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 56028   Accepted: 16112
Case Time Limit: 5000MSnode

An array of size  n ≤ 10  6 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. 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. ios

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 Inputspa

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

Sample Outputcode

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

题意: 给定一行数,共N个。有一个长度为K的窗口从左向右滑动,窗口中始终有K个数字,窗口每次滑动一个数字。求各个时刻窗口中的最大值和最小值。blog

 

1、概念介绍
一、 双端队列
双端队列是一种线性表,是一种特殊的队列,遵照先进先出的原则。双端队列支持如下4种操做:队列

(1)   从队首删除
(2)   从队尾删除
(3)   从队尾插入
(4)   查询线性表中任意一元素的值
二、 单调队列
单调队列是一种特殊的双端队列,其内部元素具备单调性。最大队列与最小队列是两种比较经常使用的单调队列,其内部元素分别是严格单调递减(不是非递增)和严格单调递增(不是非递减)的。get

单调队列的经常使用操做以下:input

(1) 插入:若新元素从队尾插入后会破坏单调性,则删除队尾元素,直到插入后再也不破坏单调性为止,再将其插入单调队列。
(2) 获取最优(最大、最小)值:访问队首元素
如下是一个单调递增队列的例子:

队列大小不能超过3,入队元素依次为3,2,8,4,5,7,6,4

3入队:(3)

3从队尾出队,2入队:(2)

8入队:(2,8)

8从队尾出队,4入队:(2,4)

5入队:(2,4,5)

2从队头出队,7入队:(4,5,7)

7从队尾出队,6入队:(4,5,6)

6从队尾出队,5从队尾出队,4从队尾出队,4入队:(4)

以上左端为队头,右端为队尾。

2、单调队列的应用
一、最大值的维护:

       好比咱们要维护一个区间为k的最大值的单调队列,因为新插入 的节点他的“生命力”确定比原先已经在队列中的元素“活”的时间长,将插入元素不断与队尾元素比, 若是他大于队尾元素,那么tail--将队尾元素删掉,(由于目前插入的这个元素值(设为pos)更大,并且“活”的时间 长,有pos在,队尾元素的有“生”之年永远都无法为最大值,故而直接无视比pos小的队尾了)。直到对空位置或者 找到了一个比pos大的队尾。 

这个问题至关于一个数据流(数列a)在不断地到来,而数据是不断过时的,至关于咱们只能保存有限的数据(sliding window中的数据,此题中就是窗口的宽度w),对于到来的查询(此题中查询是每时刻都有的),咱们要返回当前滑动窗口中的最大值最小值。注意,元素是不断过时的。

解决这个问题可使用一种叫作单调队列的数据结构,它维护这样一种队列:

a)从队头到队尾,元素在咱们所关注的指标下是递减的(严格递减,而不是非递增),好比查询若是每次问的是窗口内的最小值,那么队列中元素从左至右就应该递增,若是每次问的是窗口内的最大值,则应该递减,依此类推。这是为了保证每次查询只须要取队头元素。
b)从队头到队尾,元素对应的时刻(此题中是该元素在数列a中的下标)是递增的,但不要求连续,这是为了保证最左面的元素老是最早过时,且每当有新元素来临的时候必定是插入队尾。
知足以上两点的队列就是单调队列,首先,只有第一个元素的序列必定是单调队列。

那么怎么维护这个单调队列呢?无非是处理插入和查询两个操做。

  对于插入,因为性质b,所以来的新元素插入到队列的最后就能维持b)继续成立。可是为了维护a)的成立,即元素在咱们关注的指标下递减,从队尾插入新元素的时候可能要删除队尾的一些元素,具体说来就是,找到第一个大于(在所关注指标下)新元素的元素,删除其后全部元素,并将新元素插于其后。由于全部被删除的元素都比新元素要小,并且比新元素要旧,所以在之后的任何查询中都不可能成为答案,因此能够放心删除。

        对于查询,因为性质b,所以全部该时刻过时的元素必定都集中在队头,所以利用查询的时机删除队头全部过时的元素,在不含过时元素后,队头得元素就是查询的答案(性质a),将其返回便可。因为每一个元素都进队出队一次,所以摊销复杂度为O(n)。

模板:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 const int maxn = 1e6 + 5;
 7 struct node
 8 {
 9     int pos, val;
10 }q[maxn];
11 int k, num[maxn], n, Min[maxn], Max[maxn];
12 void getmin()
13 {
14     int head = 1, tail = 0;
15     for(int i = 1; i < k; i++)
16     {
17         while(head <= tail && num[i] < q[tail].val) tail--;
18         tail++;
19         q[tail].val = num[i];
20         q[tail].pos = i;
21     }
22     for(int i = k; i <= n; i++)
23     {
24         while(head <= tail && num[i] < q[tail].val) tail--;
25         tail++;
26         q[tail].val = num[i];
27         q[tail].pos = i;
28         while(i-q[head].pos >= k) head++;
29         Min[i-k] = q[head].val;
30     }
31 }
32 void getmax()
33 {
34     int head = 1, tail = 0;
35     for(int i = 1; i < k; i++)
36     {
37         while(head <= tail && num[i] > q[tail].val) tail--;
38         tail++;
39         q[tail].val = num[i];
40         q[tail].pos = i;
41     }
42     for(int i = k; i <= n; i++)
43     {
44         while(head <= tail && num[i] > q[tail].val) tail--;
45         tail++;
46         q[tail].val = num[i];
47         q[tail].pos = i;
48         while(i - q[head].pos >= k) head++;
49         Max[i-k] = q[head].val;
50     }
51  
52 }
53 int main()
54 {
55     while(~scanf("%d%d", &n, &k))
56     {
57         for(int i = 1; i <= n; i++) scanf("%d", &num[i]);
58         getmin();
59         for(int i=0; i<=n-k; i++) printf("%d%c",Min[i],i==n-k?'\n':' ');
60         getmax();
61         for(int i=0; i<=n-k; i++) printf("%d%c",Max[i],i==n-k?'\n':' ');
62     }
63     return 0;
64 }

用优先队列写的:操做的是下标

 1 #include<algorithm>
 2 #include<queue>
 3 #include<vector>
 4 using namespace std;
 5 int a[1000011];//数组数据
 6  
 7 int OutMin[1000011];//最小值
 8  
 9 int OutMax[1000011];//最大值
10  
11 int cnt1=0;
12 int cnt2=0;
13 int n,k;
14  
15 struct cmp1
16 {
17     bool operator()(const int a1,const int a2)
18     {
19         return a[a1]>a[a2];  //这里太精髓,重定义的是数组元素值,可是队列存的是下标
20     }
21 };
22 struct cmp2
23 {
24     bool operator()(const int a1,const int a2)
25     {
26         return a[a1]<a[a2];
27     }
28 };
29 priority_queue <int ,vector<int>,cmp1> Q1;  //重定义符号
30 priority_queue <int ,vector<int>,cmp2> Q2;
31 int main()
32 {
33     int i;
34     scanf("%d%d",&n,&k);
35     if(k>n)
36         k=n;
37     for(i=1;i<=n;++i)
38     {
39         scanf("%d",&a[i]);
40     }
41     for(i=1;i<=k;++i)
42     {
43         Q1.push(i);
44         Q2.push(i);
45     }
46     OutMin[cnt1++]=a[Q1.top()];
47     OutMax[cnt2++]=a[Q2.top()];
48     for(i=k+1;i<=n;++i)
49     {
50         Q1.push(i);
51         Q2.push(i);
52         while(i-Q1.top()>=k)
53             Q1.pop();
54         OutMin[cnt1++]=a[Q1.top()];
55         while(i-Q2.top()>=k)
56             Q2.pop();
57         OutMax[cnt2++]=a[Q2.top()];
58     }
59     
60     for(i=0;i<=(n-k);++i)
61     {
62         printf("%d%c", OutMin[i], (i < n - k) ? ' ' : '\n'); 
63         
64     }
65     for(i=0;i<=(n-k);++i)
66     {
67         printf("%d%c", OutMax[i], (i < n - k) ? ' ' : '\n'); 
68     }
69     return 0;
70 }
相关文章
相关标签/搜索