单调队列的应用
来源:互联网 发布:你见过最浪的女生知乎 编辑:程序博客网 时间:2024/05/16 03:45
Sliding Window
Time Limit: 12000MS Memory Limit: 65536KTotal Submissions: 13366 Accepted: 3708Case Time Limit: 5000MS
Description
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
单调队列指一个队列中的所有的数符合单调性(单调增或单调减),在信息学竞赛的一些题目上应用,会减少时间复杂度
开两个队列,一个维护最大值,一个维护最小值。下面叙述最大队列,最小队列的方法类似。
最大队列保证队列中各个元素大小单调递减(注意,不是单调不上升),同时每个元素的下标单调递增。这样便保证队首元素最大,而且更新的时候队首永远是当前最大。因此,这个队列需要在两头都可以进行删除,在队尾插入。
维护方法:在每次插入的时候,先判断队尾元素,如果不比待插入元素大就删除,不断删除队尾直到队尾元素大于待插入元素或者队空。删除的时候,判断队首,如果队首元素下标小于当前段左边界就删除,不断删除队首直到队首元素下标大于等于当前段左边界(注意:这时队列肯定不为空),队首元素就是当前段的最优解。
- 单调队列的应用
- 单调队列的应用
- 单调队列的应用
- HDU3530 单调队列的应用
- 浅谈单调队列的应用
- 单调队列的实现与应用
- 单调队列应用
- 单调队列 及其应用
- 单调队列及其应用
- 单调队列理解及应用
- Buy Tickets hoj 单调队列优化DP的简单应用
- Sliding Window poj 单调队列的简单应用
- POJ2823 Sliding Window (单调队列的基本应用)
- 单调队列在滑动最小值方面的应用
- 单调队列的原理
- 单调队列的学习
- 单调队列的学习 pku2823
- poj2823,典型的单调队列
- java中HashSet详解
- 开发适应中国网络的J2ME连网程序
- 不提问可以提升你的能力
- SVN 的常用命令
- 程式開發質量檢查(一)
- 单调队列的应用
- 用c# 读取网页内容
- MTK MMI event 小结 8
- java使用JMagick生成高质量缩略图
- case用法
- 利用闭包把Python函数当类使
- Visual Studio 2008操作技巧
- sql server split 字符并提取
- 10个重要的算法C语言实现源代码