diff --git a/Luogu/P5788/P5788.cpp b/Luogu/P5788/P5788.cpp index 2eb25715..bf930fdd 100644 --- a/Luogu/P5788/P5788.cpp +++ b/Luogu/P5788/P5788.cpp @@ -1,24 +1,36 @@ -#include +#include +#include -using namespace std; +using std::cin; +using std::cout; +const char endl = '\n'; -int n, a[3000005], f[3000005]; -stack st; +const int N = 3e6 + 5; + +int n, a[N], f[N]; +std::stack st; int main() { std::ios::sync_with_stdio(false); + cin.tie(nullptr); + cin >> n; + for (int i = 1; i <= n; i++) { cin >> a[i]; } - for (int i = n; i >= 1; i--) { + + for (int i = n; i; i--) { while (!st.empty() && a[st.top()] <= a[i]) st.pop(); f[i] = st.empty() ? 0 : st.top(); - st.push(i); + st.emplace(i); } + for (int i = 1; i <= n; i++) { cout << f[i] << ' '; } + cout << endl; + return 0; }