题目

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 -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

解题思路

利用两个单调队列,单调递增和单调递减,首先弄明白队尾和队首,然后当队尾比下一个值大(小)的时候移除队尾,维护队列的单调性;当队首位置加上k值小于当前i所在位置时,移除队首。

错误分析

应用scanf和printf输出

#include<iostream>
#include<deque>
#include<stdio.h>
using namespace std;int a[1010100],maxa[1010100],mina[1010100];
int main()
{deque<int >que1,que2;int n,k;scanf("%d%d",&n,&k);//cin>>n>>k;for(int i=1; i<=n; i++) scanf("%d",&a[i]);que1.push_front(1);que2.push_front(1);maxa[1]=1;mina[1]=1;k--;for(int i=2; i<=n; i++){while(que1.size()&&que1.front()+k<i) que1.pop_front();while(que1.size()&&a[que1.back()]>=a[i]) que1.pop_back();que1.push_back(i);mina[i]=que1.front();while(que2.size()&&que2.front()+k<i) que2.pop_front();while(que2.size()&&a[que2.back()]<=a[i]) que2.pop_back();que2.push_back(i);maxa[i]=que2.front();}for(int i=k+1; i<=n; i++)
//      cout<<a[mina[i]]<<" ";printf("%d ",a[mina[i]]);
//  cout<<endl;printf("\n");for(int i=k+1; i<=n; i++)
//      cout<<a[maxa[i]]<<" "; printf("%d ",a[maxa[i]]);
}

Sliding Window(单调队列)相关推荐

  1. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  2. POJ2823 Sliding Window 单调队列

    题目大意 给出一段序列,一个长度一定的窗口从左到右滑动.求窗口滑动到每个位置时窗口内数字的最大值.最小值各是多少.n<=1e6. 总体思路 遇到这种对一个沿着一个方向滑动的区间求最值问题,可以运 ...

  3. Sliding Window 单调队列解决--经典滑动窗口问题--求窗口的最大值最小值

    http://poj.org/problem?id=2823 这就要用到单调递减或单调递增队列来实现,它的头元素一直是队列中的最小值或最大值.我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素 ...

  4. POJ 2823-Sliding Window单调队列解题报告

    链接:http://poj.org/problem?id=2823 利用单调队列的出队入队,维护区间的最值,保证队列单调递增或单调递减,要维护单调递增队列,当一个数字插入的时候,从队尾往前找到第一个比 ...

  5. POJ2823 Sliding Window【单调队列】【线段树】【ST表】

    Sliding Window POJ - 2823 题意: 给出一个长度为N的序列,通过一个窗口,可以看到序列中连续的K个元素,窗口从最左边出发,每次移动一个单位,对于每次移动,输出当前窗口中的最大值 ...

  6. Sliding Window滑动窗口(单调队列)

    Sliding Window滑动窗口 POJ - 2823 目录 Sliding Window滑动窗口 POJ - 2823 题意描述 解题思路 AC代码 An array of size n ≤ 1 ...

  7. 【POJ - 2823】 Sliding Window(单调队列 用双端队列实现或模拟队列)

    题干: An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving f ...

  8. POJ 2823 Sliding Window(单调队列)

    http://poj.org/problem?id=2823 题意: 给出数组和滑动窗口的大小,每次输出滑动窗口中的最大值和最小值. 思路: 这题可以算是单调队列的模板题了,分别维护单调递增和单调递减 ...

  9. 【ST表】【单调队列】Window(jzoj 1326)

    Window jzoj 1326 题目大意 给你一个序列a和一个数k,让你求a中所有长为k的子序列的最大值和最小值 输入样例 8 3 1 3 -1 -3 5 3 6 7 输出样例 -1 -3 -3 - ...

最新文章

  1. wincc vbs mysql_Wincc VBS操作txt及SQL2005
  2. 程序员面试时,不小心说了真话…...
  3. git 撤销刚才的rebase_git rebase 的使用 (用于撤销某次commit)
  4. 2016,请不要在公司混日子!
  5. 英语中正式和休闲的打招呼_6
  6. 洛谷 P4009 汽车加油行驶问题 题解
  7. Eclipse编译时函数报错:Undefined reference to 'pthread_create'
  8. uip UDP 服务器广播模式(客户端可以任意端口,并且主动向客户端发送数据) (转)...
  9. Google API 设计指南-兼容性
  10. 分享几个百度网盘搜索引擎
  11. 算法导论习题(持续更新)
  12. 微专题:数据中心选址
  13. 基于TI Davinci架构的双核嵌入式应用处理器OMAPL138开发入门
  14. [转] 粤语八级题,你会做岩几多题??
  15. python atan_Python代码中atan()函数有什么功能呢?
  16. 图片怎么转换成PDF格式?这两种方法赶紧记下
  17. Python爬虫:Scrapy 框架快速入门及实战演练
  18. 【C语言】杨辉三角(数组)
  19. ubuntu16.04截图工具Shutter安装,设置快捷键
  20. RR 时间片轮转算法 (java)

热门文章

  1. 算法之Sunday算法
  2. 惠普孙振耀:让18位上司成为伯乐
  3. 计算机硬盘用u盘维修,硬盘坏了怎么在家自己修!
  4. U盘数据丢失怎么办?U盘数据丢失恢复方法?
  5. PC微信协议 小程序数据接收处理
  6. 【Azure Data Platform】ETL工具(13)——ADF并行加载多个文件
  7. 阿里云天池实验室训练自己的数据
  8. 嵌入式linux中断驱动,嵌入式Linux下硬件中断驱动程序的开发.pdf
  9. Linux工作计时提醒软件,使用Ubuntu命令完成定时提醒功能(预防rsi)
  10. ireport结构体介绍