一 原题
Sliding Window
| Time Limit: 12000MS | Memory Limit: 65536K | |
| Total Submissions: 68479 | Accepted: 19426 | |
| 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
二 分析
裸的滑动窗口,优先队列和双端队列都可以
三 代码
优先队列:
/*
ID: maxkibble
LANG: c++
PROB: poj 2823
*/
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 1e6 + 5;
int n, k, a[maxn], mn[maxn], mx[maxn];
struct cmp1 {
bool operator () (const int &x, const int &y) {
return a[x] > a[y];
}
};
struct cmp2 {
bool operator () (const int &x, const int &y) {
return a[x] < a[y];
}
};
priority_queue<int, vector<int>, cmp1> q1;
priority_queue<int, vector<int>, cmp2> q2;
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < k - 1; i++) {
q1.push(i); q2.push(i);
}
for (int i = k - 1; i < n; i++) {
q1.push(i); q2.push(i);
while (i - q1.top() > k - 1) q1.pop();
while (i - q2.top() > k - 1) q2.pop();
mn[i - k + 1] = a[q1.top()]; mx[i - k + 1] = a[q2.top()];
}
for (int i = 0; i < n - k + 1; i++) cout << mn[i] << " \n"[i == n - k];
for (int i = 0; i < n - k + 1; i++) cout << mx[i] << " \n"[i == n - k];
return 0;
}
双端队列:
/*
ID: maxkibble
LANG: c++
PROB: poj 2823
*/
#include <iostream>
#include <queue>
using namespace std;
#define mp make_pair
#define fi first
#define se second
typedef pair<int, int> PII;
const int maxn = 1e6 + 5;
int n, k, a[maxn], mn[maxn], mx[maxn];
deque<PII> q1, q2;
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0, j = 0; i < n - k + 1; i++) {
while (j < i + k) {
while (!q1.empty() && q1.back().fi > a[j])
q1.pop_back();
while (!q2.empty() && q2.back().fi < a[j])
q2.pop_back();
q1.push_back(mp(a[j], j));
q2.push_back(mp(a[j], j));
j++;
}
while (q1.front().se < i) q1.pop_front();
while (q2.front().se < i) q2.pop_front();
mn[i] = q1.front().fi; mx[i] = q2.front().fi;
}
for (int i = 0; i < n - k + 1; i++) cout << mn[i] << " \n"[i == n - k];
for (int i = 0; i < n - k + 1; i++) cout << mx[i] << " \n"[i == n - k];
return 0;
}

本文详细解析了滑动窗口算法的实现原理,并通过一个具体的题目示例介绍了如何使用优先队列和双端队列来高效地解决问题。文章提供了完整的代码示例,帮助读者更好地理解和应用这一算法。
255

被折叠的 条评论
为什么被折叠?



