当前位置:网站首页>单调队列模板 滑动窗口
单调队列模板 滑动窗口
2022-08-02 04:25:00 【一条小小yu】
题目描述
有一个长为 nn 的序列 aa,以及一个大小为 kk 的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值。
例如:
The array is [1,3,-1,-3,5,3,6,7][1,3,−1,−3,5,3,6,7], and k = 3k=3。
输入格式
输入一共有两行,第一行有两个正整数 n,kn,k。 第二行 nn 个整数,表示序列 aa
输出格式
输出共两行,第一行为每次窗口滑动的最小值
第二行为每次窗口滑动的最大值
输入输出样例
输入 #1复制
8 3 1 3 -1 -3 5 3 6 7
输出 #1复制
-1 -3 -3 -3 3 3 3 3 5 5 6 7
说明/提示
【数据范围】
对于 50\%50% 的数据,1 \le n \le 10^51≤n≤105;
对于 100\%100% 的数据,1\le k \le n \le 10^61≤k≤n≤106,a_i \in [-2^{31},2^{31})ai∈[−231,231)。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
typedef long long ll;
typedef double db;
int all, k;
struct node
{
int n, m;
} a[maxn];
deque<node> q;
void BIG(int all, int k)
{
for (int i = 1; i <= all; ++i)
{
while (!q.empty() && a[i].m >= q.front().m )q.pop_front(); // 求最小的
q.push_front(a[i]);
while (q.back().n <= i - k) q.pop_back();
if (i >= k) cout<<q.back().m<<" ";
}
q.clear();
}
void LOW(int all, int k)
{
for (int i = 1; i <= all; ++i)
{
while (!q.empty() && a[i].m <= q.front().m) q.pop_front(); // 求最大的
q.push_front(a[i]);
while (q.back().n <= i - k) q.pop_back();
if (i >= k) cout<<q.back().m<<" ";
}
q.clear();
}
int main()
{
cin>>all>>k;
for (int i = 1; i <= all; ++i) cin>>a[i].m, a[i].n = i;
LOW(all, k);
cout<<endl;
BIG(all, k);
return 0;
}
边栏推荐
- How to save a section of pages in a PDF as a new PDF file
- 并发性,时间和相对性(1)-确定前后关系
- 力扣练习——40 区间和的个数
- Excel skills daquan
- The line chart with square PyQt5_pyqtgraph mouse
- 张成分析(spanning test):portfolio_analysis.Spanning_test
- CaDDN code debugging
- 7亿听众背后的在线音频掘金故事
- 【STM32】ADC采集光敏数据(不看库函数手册进行配置)
- 【Interview】Recruitment requirements
猜你喜欢
被大厂强制毕业,两个月空窗期死背八股文,幸好上岸,不然房贷都还不上了
Qt编写物联网管理平台49-设备模拟工具
HyperLynx中层叠设计实例
Learn about the sequential storage structure of binary tree - heap
AFMG SysTune1.3.7使用图解
直播 | 7.30 ApacheCon Asia 2022 IOT/IIOT专题,IoTDB PMC 乔嘉林担任出品人
The line chart with square PyQt5_pyqtgraph mouse
轮询和长轮询的区别
Arduino框架下ESP32重启原因串口信息输出示例
LeetCode 23: 合并K个升序链表
随机推荐
数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析
安装部署 Kubernetes 仪表板(Dashboard)
The line chart with square PyQt5_pyqtgraph mouse
ffmpeg基本命令
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
Deep Blue Academy - Handwritten VIO Homework - Chapter 2
高等数学(第七版)同济大学 总习题三(后10题) 个人解答
Anatomy of Unreal Playback System (Part 1)
300M级mysql数据库跨版本迁移流程
AFMG SysTune1.3.7使用图解
How to save a section of pages in a PDF as a new PDF file
【面试】招聘要求
【每日一题】1374. 生成每种字符都是奇数个的字符串
8月1日“海豹数藏”将全网首发民族英雄林则徐《四行行书》数字藏品!
爬虫_爬取wasde月度供需平衡表(实例)
LeetCode 23: 合并K个升序链表
ClickHouse的客户端命令行参数
falco 【1】入门
Visual SLAM Lecture Fourteen - Lecture 13 Practice: Designing a SLAM system (the most detailed code debugging and running steps)
如何解决QByteArray添加quint16双字节时错误?