``` #include <iostream> #include <vector> #include <stack> using namespace std; int main() { int n = 0; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int val = 0; cin >> val; v.push_back(val); } stack<int> st; vector<int> left(n, -1); vector<int> right(n, -1); for (int i = 0; i < n; i++) { while (!st.empty() && v[st.top()] >= v[i]) { right[st.top()] = i; st.pop(); } if (!st.empty()) { left[i] = st.top(); } st.push(i); } for (int i = n - 1; i >= 0; i--) { if (right[i] != -1 && v[right[i]] >= v[i]) { right[i] = right[right[i]]; } } for (int i = 0; i < n; i++) { cout << left[i] << " " << right[i] << endl; } return 0; } ```</int></int></int></int></stack></vector></iostream>
点赞

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务