当前位置: 代码迷 >> 综合 >> 队列-单调队列-滑动窗口
  详细解决方案

队列-单调队列-滑动窗口

热度:10   发布时间:2024-02-25 06:47:37.0

总时间限制: 12000ms

内存限制: 65536kB

描述

给定一个长度为n(n<=10^6)的数组。有一个大小为k的滑动窗口从数组的最左端移动到最右端。你可以看到窗口中的k个数字。窗口每次向右滑动一个数字的距离。

下面是一个例子:

数组是 [1 3 -1 -3 5 3 6 7], k = 3。

 

窗口位置 最小值 最大值
[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



你的任务是得到滑动窗口在每个位置时的最大值和最小值。

 

输入

输入包括两行。
第一行包括n和k,分别表示数组的长度和窗口的大小。
第二行包括n个数字。

输出

输出包括两行。
第一行包括窗口从左至右移动的每个位置的最小值。
第二行包括窗口从左至右移动的每个位置的最大值。

样例输入

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

样例输出

-1 -3 -3 -3 3 3
3 3 5 5 6 7
#include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <deque>
using namespace std;const int MAXN = 1e6;
int n, k;
int a[MAXN+5];int main()
{scanf("%d%d", &n, &k);for (int i = 0; i < n; ++i)scanf("%d", &a[i]);/* min */deque<int> q;for (int i = 0; i < k-1; ++i) {while (!q.empty() && a[q.front()] > a[i])q.pop_front();q.push_front(i);}for (int i = k-1; i < n; ++i) {if (!q.empty() && i - q.back() >= k)q.pop_back();while (!q.empty() && a[q.front()] > a[i])q.pop_front();q.push_front(i);printf("%d ", a[q.back()]);}printf("\n");/* max */q.clear();for (int i = 0; i < k-1; ++i) {while (!q.empty() && a[q.front()] < a[i])q.pop_front();q.push_front(i);}    for (int i = k-1; i < n; ++i) {if (!q.empty() && i - q.back() >= k)q.pop_back();while (!q.empty() && a[q.front()] < a[i])q.pop_front();q.push_front(i);printf("%d ", a[q.back()]);}printf("\n");system("pause");return 0;
}

 【分析】

单调队列问题,十分巧妙

可以看看这篇题解

  相关解决方案