'commit'
This commit is contained in:
30
TangDou/LuoGuBook/P2947_2.cpp
Normal file
30
TangDou/LuoGuBook/P2947_2.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#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]; //谁仰望谁
|
||||
stack<int> stk;
|
||||
|
||||
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 (stk.size() && a[i] > a[stk.top()]) {
|
||||
s[stk.top()] = i;//仰视对象
|
||||
stk.pop();//找到了仰望对象,就不再需要继续找了
|
||||
}
|
||||
stk.push(i);//加入栈中,等待找到需它仰视的奶牛。
|
||||
}
|
||||
for (int i = 1; i <= n; i++) cout << s[i] << endl;//输出即可
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user