手撕代码:检测温度身高需要间隔几天,如果一直没有升高,就为0。温度全为整数。input: 75, 76, 58, 54, 59, 80, 48, 49 output:1, 4, 2, 1, 1, 0, 1, 0
//1.可以直接模拟。
//2.单调栈。记录一下
#include<iostream>
#include<stack>
using namespace std;
const int N = 1010;
// 8
// 75 76 58 54 59 80 48 49
//1, 4, 2, 1, 1, 0, 1, 0
int n;
int p[N], l[N];
int main()
{
cin >> n;
stack<int> sta;
for (int i = 1; i <= n; i ++ ) cin >> p[i];
for(int i = n; i >= 1; i -- )
{
while (!sta.empty() && p[sta.top()] <= p[i]) sta.pop();
if (sta.empty()) l[i] = n + 1;
else l[i] = sta.top();
sta.push(i);
}
for (int i = 1; i <= n; i ++ )
{
int num = l[i] == n + 1 ? 0 : l[i] - i;
cout << num << " ";
}
return 0;
}