Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 46492 | Accepted: 13438 | |
Case Time Limit: 5000MS |
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.
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
Source
POJ Monthly--2006.04.28, Ikki
题意:
长度为n的一个数组,问每一个长度为k的连续的子序列中元素的最大值和最小值
思路:
单调队列的模板题,用两个单调队列分别保存最大值和最小值
单调队列可以进行如下操作:
入队:将元素入队,保持队列的单调性,单调队列中所有比x劣的值都会被去掉。由于被发现与将入队元素相比更劣的元素都会被去掉,这样限制了比较的次数,而且每个元素只会出队一次,将n个元素全部入队一次的复杂度为O(n);
出队:将最老的元素出队,复杂度O(1)
查询:查询队首元素,即所维护的最值,复杂度O(1)
因此可以以O(n)的时间复杂度解决这个问题
/***********************************
* POJ 2823 Sliding Window 单调队列
* 22696K 6188MS C++ 2032B 2015-07-27 09:49:19
* 长度为n的一个数组,问每一个长度为k的连续的子序列中元素的最大值和最小值
* 单调队列的模板题,用两个单调队列分别保存最大值和最小值
* 时间空间复杂度O(n)
************************************/
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX = (int)1e6 + 10;
typedef pair<int, int> PII;///first为数组元素,second为对应下标
class Queue {
private:
PII data[MAX];///存放数组元素及其对应下标,保证最值在head处
PII* head, *tail;///单调队列的队首队尾指针
int headAll, tailAll;///保存整个队列的队首队尾
bool(*dataCompare)(const PII& a, const PII& b);///比较函数,true说明a更优,应当替换调b
public:
Queue(bool(*compare)(const PII& a, const PII& b)) {
head = data;
tail = data;
headAll = tailAll = 0;
dataCompare = compare;
}
///将元素x入队,单调队列中所有比x劣的值都会被去掉
void push(int x) {
PII* prt;
PII t = make_pair(x,tailAll++);
///从队尾开始比较,如果劣于x则继续向前移动,直到移动到队首或者第一个优于x的值的后方
for(prt = tail; prt - 1 >= head && dataCompare(t,*(prt - 1)); --prt)
;
///插入x,并去掉x之后的所有元素
*prt = t;
tail = prt + 1;
}
///出队,如果要出队的元素正处于单调队列的队首,将其出队
void pop(void) {
if (headAll >= tailAll) {
return;
}
if (headAll == head->second) {
head++;
}
headAll++;///维护整个队列的队首
}
const int& top(void) {
return head->first;
}
};
inline bool compareMax(const PII& a, const PII& b) {
return a.first >= b.first;///值相等,后进的比先进的优,因为我总是将后进的为a,所以取=
}
inline bool compareMin(const PII& a, const PII& b) {
return a.first <= b.first;
}
Queue Max(compareMax);
Queue Min(compareMin);
int ans1[(int)1e6 + 10];
int ans2[(int)1e6 + 10];
int main(void) {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
Max.push(x);
Min.push(x);
///单调队列中的元素已有k,开始记录答案并出队
if (i >= k - 1) {
ans1[i] = Min.top();
Min.pop();
ans2[i] = Max.top();
Max.pop();
}
}
for (int i = k - 1; i < n; ++i) {
printf("%d%c", ans1[i], i == n - 1 ? '\n' : ' ');
}
for (int i = k - 1; i < n; ++i) {
printf("%d%c", ans2[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}