题目描述
Farmer John’s N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again standing in a row. Cow i has height H_i (1 <= H_i <= 1,000,000).
Each cow is looking to her left toward those with higher index numbers. We say that cow i ‘looks up’ to cow j if i < j and H_i < H_j. For each cow i, FJ would like to know the index of the first cow in line looked up to by cow i.
Note: about 50% of the test data will have N <= 1,000.
约翰的N(1≤N≤10^5)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向右看齐.对于奶牛i,如果奶牛j满足i
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
int h[100001],ans[100001],s[100001];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
}
int top=0;
s[++top]=1;
for(int i=2;i<=n;i++){
while(h[i]>h[s[top]]&&top>0){
ans[s[top]]=i;
top--;
}
top++;
s[top]=i; }
while(top>0){ans[s[top]]=0;top--;}
for(int i=1;i<=n;i++){
cout<<ans[i]<<endl;
}
return 0;
}