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 个整数,每次窗体向右移动一位,如下表:
随机推荐
- CVE-2015-5531(目录遍历漏洞)
vulhub漏洞环境搭建 https://blog.csdn.net/qq_36374896/article/details/84102101 启动docker环境 cd vulhub-master/ ...
- python 输入加密的MD5值,并搜索文件中的原始值
此程序可以输入一串MD5的值,并在指定的文件中搜索到原始值.程序自有用处. #输入加密的MD5值,并搜索文件中的原始值 by qianxiao996 #博客地址:https://blog.csdn.n ...
- TetBrains产品快捷键大全
快捷键大全
- crash_for_windows_pkg远程代码执行漏洞
漏洞详情 crash_for_windows_pkg由 Electron 提供支持.如果 XSS 负载以代理的名义,我们可以远程执行受害者计算机上的任何 JavaScript 代码. 受影响的冲突版本 ...
- Water 2.5.6 发布,一站式服务治理平台
Water(水孕育万物...) Water 为项目开发.服务治理,提供一站式解决方案(可以理解为微服务架构支持套件).基于 Solon 框架开发,并支持完整的 Solon Cloud 规范:已在生产环 ...
- sleep 和 wait 的区别?
Sleep是休眠线程,wait是等待,sleep是thread的静态方法,wait则是object的方法. Sleep依旧持有锁,并在指定时间自动唤醒.wait则释放锁.
- kafka 分布式(不是单机)的情况下,如何保证消息的顺序消费?
Kafka 分布式的单位是 partition,同一个 partition 用一个 write ahead log 组织, 所以可以保证 FIFO 的顺序.不同 partition 之间不能保证顺序. ...
- Mybatis框架基础入门(六)--动态sql
主要是通过mybatis提供的各种标签方法实现动态拼接sql. 1.if标签 <!-- 根据条件查询用户 --> <select id="queryUserByWhere& ...
- 使用SpringDataJdbc无法注册的情况
当 EnableJdbcRepositories 注解无法注册Repository仓库的时候,你可以查看下 你的实体是否存在@Table注解,没有请加上,这样就能扫描到了 @Table("b ...
- GC root & 使用MAT分析java堆
当我们的java程序遇到频繁full gc或者oom的时候,我们常常需要将当前的heap dump出来进行进一步的分析.MAT是用于分析heap dump的神器. 1 生成heap dump heap ...
