-
Notifications
You must be signed in to change notification settings - Fork 80
/
MinMaxMiddle.cpp
60 lines (50 loc) · 1002 Bytes
/
MinMaxMiddle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;++i) {
cin>>a[i];
}
stack<int> s;
int left[n],right[n],ans[n+1],len;
for(i=0;i<n;++i) {
left[i]=-1,right[i]=n;
}
for(i=0;i<n;++i) {
while(!s.empty() && a[s.top()] >= a[i]) {
s.pop();
}
if(!s.empty()) {
left[i]=s.top();
}
s.push(i);
}
while(!s.empty()) {
s.pop();
}
for(i=n-1;i>=0;--i) {
while(!s.empty() && a[s.top()] >= a[i]) {
s.pop();
}
if(!s.empty()) {
right[i]=s.top();
}
s.push(i);
}
memset(ans, 0, sizeof ans);
for(i=0;i<n;++i) {
len = right[i]-left[i]-1;
ans[len]=max(ans[len], a[i]);
}
for(i=n-1;i>=1;--i) {
ans[i]=max(ans[i], ans[i+1]);
}
for(i=1;i<=n;++i) {
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}