数据结构
单调栈:
#include <iostream> #include <stack> using namespace std; stack<int> st; const int maxn = 1e6 + 7; int a[maxn], n, list[maxn]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { while (st.size() && a[st.top()] >= a[i]) st.pop(); if (st.size()) list[i] = st.top(); else list[i] = 0; st.push(i); cout << list[i] << "\t"; } cout << endl; while(st.size()) st.pop(); for (int i = n; i >= 1; i--) { while (st.size() && a[st.top()] >= a[i]) st.pop(); if (st.size()) list[i] = st.top(); else list[i] = n + 1; st.push(i); cout << list[i] << "\t"; } }
单调队列:
二维单调队列:
rmq+二分代替: