题意:
现在有n个人站成一行,告诉你每个人的身高。
现在每个人都要找到在他左边,比他矮的人中最高的人的位置。
同时也要找到,在他右边比他矮的人中最高的人的位置。
注意由于他们是站成一行的,所以他们不能越过比他们高的人去看后面的人。也就是说,他只能看到他本人和他的左边(或右边)第一个比他高的人之间的那些人。
请输出每个人左边比他矮的人中最高的人的位置,以及每个人的右边比他矮的人中最高的人的位置(没有的话输出0,位置从1开始)
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn=1e6+5;
int a[maxn],l[maxn],r[maxn];
int main() {
int t,kase=0;
cin>>t;
while(t--) {
int n;
cin>>n;
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
for(int i=1; i<=n; i++)cin>>a[i];
stack<int>s;
//s.push(0);
for(int i=1; i<=n; i++) {
l[i]=0;
while(!s.empty()&&a[s.top()]<a[i]) {
//s.pop();
l[i]=s.top();
s.pop();
}
s.push(i);
}
while(!s.empty())s.pop();
for(int i=n; i>=1; i--) {
r[i]=0;
while(!s.empty()&&a[s.top()]<a[i]) {
//s.pop();
r[i]=s.top();
s.pop();
}
s.push(i);
}
printf("Case %d:\n",++kase);
for(int i=1; i<=n; i++)
cout<<l[i]<<" "<<r[i]<<endl;
}
}