竞赛讨论区 > C题单调栈
头像
Down
编辑于 2018-09-07 15:23
+ 关注

C题单调栈

C题单调栈O(n)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n;
    cin >> n;
    vector<ll> a(n),b(n);
    for (ll &e : a) cin >> e;
    stack<int> stk;
    for (int i = 0;i < n; ++i)
    {
        while(!stk.empty() && a[stk.top()] < a[i])
        {
            b[stk.top()] = i + 1;
            stk.pop();
        }
        stk.push(i);
    }
    for(ll e : b) cout << e << " ";
    cout << endl;
    return 0;
}

全部评论

(0) 回帖
加载中...
话题 回帖

等你来战

查看全部

热门推荐