单调队列 - 滑动窗口
作者:小教学发布时间:2023-10-01分类:程序开发学习浏览:247
导读: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;
}
- 程序开发学习排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


