Look Up S
一开始想到暴力,但显然超时,所以换一个思路,不是考虑这头奶牛需要仰望谁,考虑这头奶牛被谁仰望,于是就会想到维护一个单调递减的栈,将比栈首低的奶牛入栈,遇到比栈首高的就出栈,直到栈首比这头奶牛高为止,再将这头奶牛入栈,等所有奶牛都完成后,还剩在栈里的就是没有仰望对象的奶牛。
`#include<stdio.h>
include
include
using namespace std;
int main(){
int n,lst[100007],st[100007],top=1;
int flag[100007]={0};
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&lst[i]);
}
st[0]=0;
for(int i=1;i<n;i++){
if(lst[i]<=lst[st[top-1]]){
st[top]=i;
top++;
}
else{
while(lst[i]>lst[st[top-1]]&&top>0){
flag[st[top-1]]=i;
top--;
}
st[top]=i;
top++;
}
}
for(int i=0;i<n;i++){
if(flag[i]>0) printf("%d\n",flag[i]+1);
else printf("%d\n",flag[i]);
}
return 0;
} `