题意:n个点,求出每个点的左边,右边,第一个比他高的点。
思路:单调栈。
void solve(){
int n;
cin >> n;
vector<int> a(n);
for (auto&x : a){
cin >> x;
}
stack<int> stk;
vector<pair<int, int>> ans(n, {-1, -1});
stk.push(0);
for (int i = 1; i < n; ++i){
while (!stk.empty() && a[stk.top()] <= a[i]){
stk.pop();
}
if (!stk.empty()){
ans[i].first = stk.top() + 1;
}
stk.push(i);
}
while (!stk.empty()){
stk.pop();
}
stk.push(n - 1);
for (int i = n - 2; i >= 0; --i){
while (!stk.empty() && a[stk.top()] <= a[i]){
stk.pop();
}
if (!stk.empty()){
ans[i].second = stk.top() + 1;
}
stk.push(i);
}
for (int i = 0; i < n; ++i){
cout << ans[i].first << " \n"[i == n - 1];
}
for (int i = 0; i < n; ++i){
cout << ans[i].second << " \n"[i == n - 1];
}
}
总结:为什么stack没有clear呢?
标签:百亿,int,top,富翁,stk,蓝桥,&&,stack From: https://www.cnblogs.com/yxcblogs/p/18103041