【数据结构】[luoguP1886]滑动窗口

前端之家收集整理的这篇文章主要介绍了【数据结构】[luoguP1886]滑动窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

题目

接近于单调队列的模板了 根据大小之类的入队出队 代码好理解
代码如下

#include<iostream> #include<cstdio> #include<cctype> using namespace std; #define in = read() typedef long long ll; const ll size = 1000000 + 1000; ll n,k; ll a[size]; ll h,t; ll q[size],p[size]; inline ll read(){ ll num = 0,f = 1; char ch = getchar(); while (!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)){ num = num*10 + ch - '0'; ch = getchar(); } return num*f; } inline void maxx(){ h = 1; t = 0; for (int i=1;i<=n;i++){ while (h <= t && q[t] <= a[i]) t --; q[++ t] = a[i]; p[t] = i; while (p[h] <= i - k) h ++; if(i >= k) printf("%d ",q[h]); } printf("\n"); } inline void minn(){ h =1; t = 0; for (int i=1;i<=n;i++){ while (h <= t && q[t] >= a[i]) t --; q[++ t] = a[i]; p[t] = i; while(p[h] <= i - k) h ++; if(i >= k) printf("%d ",q[h]); } printf("\n"); } int main(){ n in; k in; for (int i=1;i<=n;i++) a[i] in; minn(); maxx(); } //COYG 
原文链接:https://www.f2er.com/datastructure/382287.html

猜你在找的数据结构相关文章