POJ2823 滑动窗口 单调队列
来源:互联网 发布:android ble 接收数据 编辑:程序博客网 时间:2024/04/28 01:57
An array of size n ≤ 106 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.Window position Minimum value Maximum value [1 3 -1] -3 5 3 6 7 -13 1 [3 -1 -3] 5 3 6 7 -33 1 3 [-1 -3 5] 3 6 7 -35 1 3 -1 [-3 5 3] 6 7 -35 1 3 -1 -3 [5 3 6] 7 36 1 3 -1 -3 5 [3 6 7]37
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
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 31 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 33 3 5 5 6 7
曾经在leetcode上做过这个题目,但是,为了学习一种新的数据结构,单调队列,重新做了这道题目
单调队列,需要维护,一个pair,分别是元素的pos,和val,队列内部是按照,val升序(或降序)排列的,我们维护最小值,因此,队尾元素入队的时候,可以删除比他大的所有元素,同时判断队首元素的pos是不是超出了范围,即时的pop_front(),最后C++过了,G++ TLE,莫名其妙。
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;#define N 1000007#include <deque>int n, k;int a[N];struct Node { int pos,val; Node(){} Node(int p,int v):pos(p), val(v) {}};int main() { //freopen("in.txt", "r", stdin); while (~scanf("%d%d",&n, &k)) { for (int i=0; i<n; i++) { scanf("%d", &a[i]); } deque<Node> d; //max for (int i=0; i<k; i++) { //加入队尾 while (d.empty() == false) { if (d.back().val >= a[i]) { d.pop_back(); } else { break; } } d.push_back(Node(i, a[i])); } printf("%d ",d.begin()->val); for (int i=k; i<n; i++) { //加入队尾 while (d.empty() == false) { if (d.back().val >= a[i]) { d.pop_back(); } else { break; } } d.push_back(Node(i, a[i])); //处理队首 while (d.empty() == false) { if (i - d.begin()->pos >= k) { d.pop_front(); } else { break; } } printf("%d ",d.begin()->val); } d.clear(); printf("\n"); //min for (int i=0; i<k; i++) { //加入队尾 while (d.empty() == false) { if (d.back().val <= a[i]) { d.pop_back(); } else { break; } } d.push_back(Node(i, a[i])); } printf("%d ",d.begin()->val); for (int i=k; i<n; i++ ) { //加入队尾 while (d.empty() == false) { if (d.back().val <= a[i]) { d.pop_back(); } else { break; } } d.push_back(Node(i, a[i])); //处理队首 while (d.empty() == false) { if (i - d.begin()->pos >= k) { d.pop_front(); } else { break; } } printf("%d ",d.begin()->val); } d.clear(); }}
0 0
- POJ2823 滑动窗口 单调队列
- poj2823(滑动窗口,单调队列模板)
- 单调队列与滑动窗口(Sliding window, poj2823)
- POJ2823 滑动窗口中元素的最值(单调队列)
- POJ2823 滑动窗口 单调队列模板题 第一次用了发函数指针
- 滑动窗口--单调队列
- 单调队列--滑动窗口
- poj2823 单调队列
- 单调队列 POJ2823
- 单调队列 (POJ2823)
- POJ2823单调队列
- poj2823单调队列
- POJ2823 单调队列
- poj2823(单调队列)
- poj2823(单调队列)
- 单调队列-poj2823
- POJ2823(单调队列)
- poj2823(单调队列)
- Sqoop-1.4.5用户手册
- jQuery插件之Cookie
- "围观"设计模式(30)--结构型设计模式总结(适配器、代理、装饰、外观、桥梁、组合、享元)
- VS2010:error C2061: 语法错误
- JS取得Tree的ID
- POJ2823 滑动窗口 单调队列
- BZOJ 1878 HH的项链(离线树状数组)
- UVA - 12295 Optimal Symmetric Paths
- 贪心法__动态规划-数塔问题
- js中的this和prototype
- Python JSon实例1
- AsyncTask 和 Handler的优缺点
- 【bzoj3938】Robot 超哥线段树
- 淘宝产品外壳收集