题意
题面太复杂了,我们先简化一下题面:
有 个人在排队,第 个人的身高是 ,输出从前往后到第 个人由高到低的有多少个人,即题面所说的「不安度」。
看到简化体面中的“越来越高”,就说明我们需要输出容器中从大到小的数列的长度。不难发现,容器是单调栈,接下来,只需要满足栈中数是从大到小的即可。
代码
#include <bits/stdc++.h>
using namespace std;
stack <int> s;
int a[100005], n;
int main () {
ios :: sync_with_stdio (0);
cin.tie (0), cout.tie (0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cout << s.size () << endl;
while (s.size () && s.top () < a[i]) s.pop ();
s.push (a[i]);
}
return 0;
}