#pragma GCC target("avx")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e6 + 10;
int a[N], l[N], r[N], ans[N];
int n, m, k;
void solve(int t){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
r[i] = n + 1;
}
stack<int> s;
for(int i = 1; i <= n; i++){
while(s.size() && a[s.top()] > a[i]){
//找到顶点右边比顶部小的第一个位置
r[s.top()] = i;
s.pop();
}
s.push(i);
}
while(s.size()){
s.pop();
}
for(int i = n; i; i--){
while(s.size() && a[s.top()] > a[i]){
//找到顶点左边比顶部小的第一个位置
l[s.top()] = i;
s.pop();
}
s.push(i);
}
for(int i = 1; i <= n; i++){
//li + 1 到 ri - 1 的范围内, ai是最小的
ans[r[i] - l[i] - 1] = max(ans[r[i] - l[i] - 1], a[i]);
}
for(int i = n; i; i--){
ans[i] = max(ans[i], ans[i + 1]);
}
for(int i = 1; i <= n; i++){
cout << ans[i] << " ";
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
for(int i = 1; i <= t; i++){
solve(i);
}
return 0;
}
木板效应[单调队列]
于 2022-10-20 22:20:14 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)