mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-23 18:28:47 +00:00
parent
e97574f3e5
commit
24213fc267
37
Luogu/P2947/P2947.cpp
Normal file
37
Luogu/P2947/P2947.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
const char endl = '\n';
|
||||
|
||||
const int N = 1e5 + 5;
|
||||
|
||||
int n, h[N], ans[N];
|
||||
std::stack<int> st;
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
cin >> n;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cin >> h[i];
|
||||
}
|
||||
|
||||
for (int i = n; i; i--) {
|
||||
// pop all elements that are smaller than h[i]
|
||||
while (!st.empty() && h[i] >= h[st.top()]) st.pop();
|
||||
// if the stack is not empty, the nearest element that is larger than h[i] is st.top()
|
||||
if (!st.empty()) ans[i] = st.top();
|
||||
// push h[i] into the stack
|
||||
st.push(i);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
cout << ans[i] << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user