题目描述
思路:
现将以大小排序,当前位置到离它最远位置用单调栈维护
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, ans, q[101010];
struct node
{
int x, id;
}a[101010];
bool cmp(node x, node y)
{
if(x.x!=y.x)
return x.x>y.x;
else return x.id>y.id;
}
int main()
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%d", &a[i].x), a[i].id=i;
sort(a+1, a+1+n, cmp);
int tl=0;
for(int i=1; i<=n; i++)
{
while(a[i].id>a[q[tl]].id&&tl!=0)
tl--;
q[++tl]=i;
ans=max(ans, a[q[1]].id-a[i].id);
}
printf("%d", ans);
return 0;
}