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) 回帖