Sliding Window - 题解【单调队列】
题面:
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.
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 Input8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
rep(i, 1, n) {//for(int i=1,i<=n;++i)
while (!que.empty() && que.back() > m[i]) {
que.pop_back();
num.pop_back();//在这里我们需要记录下每一个元素的索引,来判断窗口移动过程中是否需要出队
}
que.push_back(m[i]);
num.push_back(i);
if (num.front() < i - k + 1) {
que.pop_front();
num.pop_front();//进行出队操作
}
if (i >= k) {
printf("%d ", que.front());//输出队列头部元素(由单调队列性质,一定是最小值)
}
}
#include <iostream>
#include <cstdio>
#include <climits>
#include <deque>
#include <algorithm>
#define grp int T;cin>>T;while(T--)
#define elif else if
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
int n, k;
int m[1000010];
deque<int> que;
deque<int> num;
int main() {
scanf("%d %d", &n, &k);
rep(i, 1, n) {
scanf("%d", &m[i]);
}
rep(i, 1, n) {
while (!que.empty() && que.back() > m[i]) {
que.pop_back();
num.pop_back();
}
que.push_back(m[i]);
num.push_back(i);
if (num.front() < i - k + 1) {
que.pop_front();
num.pop_front();
}
if (i >= k) {
printf("%d ", que.front());
}
}
putchar('\n');
que.clear();
num.clear();
rep(i, 1, n) {
while (!que.empty() && que.back() < m[i]) {
que.pop_back();
num.pop_back();
}
que.push_back(m[i]);
num.push_back(i);
if (num.front() < i - k + 1) {
que.pop_front();
num.pop_front();
}
if (i >= k) {
printf("%d ", que.front());
}
}
putchar('\n');
return 0;
}
Sliding Window - 题解【单调队列】的更多相关文章
- 【POJ 2823】Sliding Window(单调队列/堆)
BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...
- 题解报告:poj 2823 Sliding Window(单调队列)
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- POJ 2823 Sliding Window(单调队列 || 线段树)题解
题意:求每个长度为k的数组的最大值和最小值 思路: 1.用线段树创建维护最大值和最小值,遍历询问,简单复习了一下...有点手生 2.单调队列: 可以看一下详解 单调队列顾名思义就是一个单调递增或者递减 ...
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- POJ2823 Sliding Window(单调队列)
题目要输出一个序列各个长度k的连续子序列的最大值最小值. 多次RMQ的算法也是能过的,不过单调队列O(n). 这题,队列存元素值以及元素下标,队尾出队维护单调性然后入队,队首出队保持新元素下标与队首元 ...
- poj 2823 Sliding Window(单调队列)
/* 裸地单调队列.. 第一次写 写的好丑.... */ #include<iostream> #include<cstdio> #include<cstring> ...
- 【POJ 2823 Sliding Window】 单调队列
题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...
- POJ 2823 Sliding Window 【单调队列】
题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...
- 【Sliding Window】单调队列
题目描述 给你一个长度为 N 的数组,一个长为 K 的滑动的窗体从最左移至最右端,你只能见到窗口的 K 个整数,每次窗体向右移动一位,如下表:
随机推荐
- 3、Lambda表达式
Lambda表达式 Lambda表达式(lambda expression),是一种匿名函数,即没有函数名的函数. Lambda表达式不仅在C#中使用,在Java.Phtyon.C++ 中都有使用. ...
- 你了解过Servlet3.0吗?
Servlet3.0相对于Servlet2.0来说最大的改变是引入了Annotation标注来取代xml配置,用于简化web应用的开发和部署.最主要几项特性: 1. 新增的注解支持:该版本新增了若干注 ...
- 详细描述一下 Elasticsearch 搜索的过程?
想了解 ES 搜索的底层原理,不再只关注业务层面了. 解答: 搜索拆解为"query then fetch" 两个阶段. query 阶段的目的:定位到位置,但不取. 步骤拆解如下 ...
- ubuntu18.04设置开机自启Django
设置开机自启: rc-local.server [Unit] Description=/etc/rc.local Compatibility ConditionPathExists=/etc/rc.l ...
- 构造器(constructor)是否可被重写(override)?
构造器不能被继承,因此不能被重写,但可以被重载.
- 如何获取 topic 主题的列表?
bin/kafka-topics.sh --list --zookeeper localhost:2181
- Mybatis框架基础入门(一)--简介及优势
一.什么是Mybatis 这里借用官网的一句话介绍什么是mybatis:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC ...
- memcached 如何实现冗余机制?
不实现!我们对这个问题感到很惊讶.Memcached 应该是应用的缓存层.它的设 计本身就不带有任何冗余机制.如果一个 memcached 节点失去了所有数据,您 应该可以从数据源(比如数据库)再次获 ...
- 在java web工程中jsp页面中使用kindeditor
在这之前我们用Notepad++写过kindeditor 在Java web工程里也差不多 首先我们复制之前的thml代码粘贴到工程里 然后把样式也复制进去 然后就可以运行了
- mybatis 和 hibernate 本质区别和应用场景
Hibernate: 是一个标准 ORM 框架(对象关系映射).入门门槛较高,不需要程序员写 SQL,SQL语句自动生成. 对 SQL 语句进行优化.修改比较困难. 应用场景: 适用于需求变化不多的中 ...
