单调队列 - 滑动窗口
作者:小教学发布时间:2023-10-01分类:程序开发学习浏览:117
导读: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;
}
- 程序开发学习排行
- 最近发表
-
- PSV官方英日文游戏精选集
- Digital Marketing Freelancer | WordPress Theme 博客主题下载
- Pixel Hair Spa | WordPress Theme 博客主题下载
- Aster Travel Blog | WordPress Theme 博客主题下载
- Urbana | WordPress Theme 博客主题下载
- WENS Next Gen Blog | WordPress Theme 博客主题下载
- PSV游戏最简安装教程
- PSV中文游戏全集
- Otter Blocks Gutenberg Blocks,Page Builder for Gutenberg Editor amp; FSE 最好的WordPress常用插件下载 博客插件模块
- 支付表格、立即购买按钮和发票系统|GetPaid 最好的WordPress通用插件下载 博客插件模块