单调队列 - 滑动窗口
作者:小教学发布时间:2023-10-01分类:程序开发学习浏览:87
导读:154.滑动窗口-AcWing题库O(1)求窗口中的最大值/最小值#include<bits/stdc++.h>#defineIOSios::s...
154. 滑动窗口 - AcWing题库
O(1)求窗口中的最大值/最小值
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef long double ld;
const int N = 1000010;
int a[N], q[N];
int main()
{
IOS
int n, k;
cin >> n >> k;
for(int i = 1; i <= n; i ++)cin >> a[i];
int hh = 0, tt = -1;
for(int i = 1; i <= n; i ++)
{
if(hh <= tt && q[hh] < i - k + 1)hh ++;
while(hh <= tt && a[q[tt]] >= a[i])tt --;
q[++ tt] = i;
if(i >= k)cout << a[q[hh]] << ' ';
}
cout << endl;
hh = 0, tt = -1;
for(int i = 1; i <= n; i ++)
{
if(hh <= tt && q[hh] < i - k + 1)hh ++;
while(hh <= tt && a[q[tt]] <= a[i])tt --;
q[++ tt] = i;
if(i >= k)cout << a[q[hh]] << ' ';
}
return 0;
}
- 程序开发学习排行
- 最近发表
-
- Wii官方美版游戏Redump全集!游戏下载索引
- 视觉链接预览最好的WordPress常用插件下载博客插件模块
- 预约日历最好的wordpress常用插件下载博客插件模块
- 测验制作人最好的WordPress常用插件下载博客插件模块
- PubNews Plus|WordPress主题博客主题下载
- 护肤品|wordpress主题博客主题下载
- 肯塔·西拉|wordpress主题博客主题下载
- 酷时间轴(水平和垂直时间轴)最好的wordpress常用插件下载博客插件模块
- 作者头像列表/阻止最好的wordPress常用插件下载博客插件模块
- Elementor Pro Forms最好的WordPress常用插件下载博客插件模块的自动完成字段