You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

#include <bits/stdc++.h>
//奶牛的仰望
//https://www.cnblogs.com/littlehb/p/15247842.html
using namespace std;
const int N = 101000;
int n; //多少只奶牛
int a[N]; //每只奶牛的身高
int s[N]; //谁仰望谁
//用数组模拟栈,每一个栈内的元素,都是需要找到“比自己高度大的,离自己最近”的奶牛。
int stk[N]; //内容:序号
int tt; //指针,默认是0
int main() {
//优化不可少
ios::sync_with_stdio(false);
//读入
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
//奶牛一个个进来,判断新进来的是不是已有奶牛仰望的对象
for (int i = 1; i <= n; i++) {
//栈内有奶牛,且身高大于栈顶的奶牛
while (tt && a[i] > a[stk[tt]]) {
s[stk[tt]] = i;//仰视对象
tt--;//出栈
}
stk[++tt] = i;//加入栈中,等待找到需它仰视的奶牛。
}
for (int i = 1; i <= n; i++) cout << s[i] << endl;//输出即可
return 0;
}