0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 18:16:26 +00:00

P5788 【模板】单调栈

https://www.luogu.com.cn/record/91963289
This commit is contained in:
Baoshuo Ren 2022-10-28 22:04:10 +08:00
parent 91637b0b00
commit e72bb88470
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,24 +1,36 @@
#include <bits/stdc++.h> #include <iostream>
#include <stack>
using namespace std; using std::cin;
using std::cout;
const char endl = '\n';
int n, a[3000005], f[3000005]; const int N = 3e6 + 5;
stack<int> st;
int n, a[N], f[N];
std::stack<int> st;
int main() { int main() {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n; cin >> n;
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
cin >> a[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(); while (!st.empty() && a[st.top()] <= a[i]) st.pop();
f[i] = st.empty() ? 0 : st.top(); f[i] = st.empty() ? 0 : st.top();
st.push(i); st.emplace(i);
} }
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
cout << f[i] << ' '; cout << f[i] << ' ';
} }
cout << endl; cout << endl;
return 0; return 0;
} }