1.题目描述:
Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 59703 | Accepted: 17128 | |
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
2.题意概述:
给定一个大小已知的数组以及一个大小已知的滑动窗口,窗口每个时刻向后移动一位,求出每个时刻窗口中数字的最大值和最小值。
3.解题思路:
暴力查找复杂度是n*k肯定是不行的,考虑用单调队列优化:
求最大值:建立一个单调递减队列,元素从左到右依次入队,入队之前必须从队列尾部开始删除那些比当前入队元素小或者相等的元素,直到遇到一个比当前入队元素大的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。
求最小值:建立一个单调递增队列,元素从左到右依次入队,入队之前必须从队列发问开始删除那些比当前入队元素大或者相等的元素,直到遇到一个比当前入队元素小的元素,或者队列为空为止。若此时队列的大小超过窗口值,则从队头删除元素,直到队列大小小入窗口值为止。然后把当前元素插入队尾。
设窗口大小为k,本题解法为建立两个单调队列,先从原数组中取前k个元素按上述要求入队,然后获取队头元素,再把下一个元素放入队中,再获取头元素,这样一直到最后一个元素为止。最后把答案从头到尾依次输出即可。
4.AC代码:#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 1000100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
struct node
{
int id, num;
} q[maxn];
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, k;
while (~scanf("%d%d", &n, &k))
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int head = 1, tail = 1;
q[0].num = -INF;
//单调递增,求最小值
for (int i = 1; i <= k; i++)
{
while (tail >= head && a[i] <= q[tail].num)
tail--;
q[++tail].num = a[i];
q[tail].id = i;
}
for (int i = k; i <= n; i++)
{
while (tail >= head && a[i] <= q[tail].num)
tail--;
q[++tail].num = a[i];
q[tail].id = i;
while (q[head].id <= i - k)
head++;
if (i == k)
printf("%d", q[head].num);
else
printf(" %d", q[head].num);
}
puts("");
head = tail = 0;
q[0].num = INF;
// 单调递减,求最大值
for (int i = 1; i <= k; i++)
{
while (tail >= head && a[i] >= q[tail].num)
tail--;
q[++tail].num = a[i];
q[tail].id = i;
}
for (int i = k; i <= n; i++)
{
while (tail >= head && a[i] >= q[tail].num)
tail--;
q[++tail].num = a[i];
q[tail].id = i;
while (q[head].id <= i - k)
head++;
if (i == k)
printf("%d", q[head].num);
else
printf(" %d", q[head].num);
}
puts("");
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}