2947 look up s
每只奶牛都朝有看齐,求每一只奶牛的最近的仰望对象,很明显,这是一单调栈的模板,没有什么好解释的,输出的时候需要换行,并且输出的是编号
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<stack>
using namespace std;
const int SIZE=3e6+5;
const int inf=9999999;
int a[SIZE],answer[SIZE];
stack<int> s;
int main() {
int n;
cin>>n;
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
//单调递增
for(int i=1; i<=n; i++) {
if(s.empty()||a[i]<=a[s.top()]) s.push(i);//存储编号
else {
while(s.size()&&a[i]>a[s.top()]) {
answer[s.top()]=i;
s.pop();
}
s.push(i);
}
}
for(int i=1; i<=n; i++)
printf("%d\n",answer[i]);
return 0;
}