为什么我#9 TLE
查看原帖
为什么我#9 TLE
1083200
guojiahong楼主2024/3/22 13:13

C++

#include<bits/stdc++.h>
using namespace std;
int a[100005];
stack<int> s1,s2;
int ans1[100005],ans2[100005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
	{
		while(!s1.empty()&&a[s1.top()]>=a[i])
		{
			ans1[s1.top()]=i-s1.top();
			s1.pop();
		}
		s1.push(i);
	}
	while(!s1.empty())
	{
		ans1[s1.top()]=n-s1.top()+1;
		s1.pop();
	}
	for(int i=n;i>=0;i--)
	{
		while(!s2.empty()&&a[s2.top()]<=a[i])
		{
			ans2[s2.top()]=s2.top()-i;
			s2.pop();
		}
		s2.push(i);
	}
	while(!s2.empty())
	{
		ans2[s2.top()]=s2.top();	
		s2.pop();
	}
	int maxx=0;
	for(int i=n;i>0;i--)
		for(int j=ans1[i]-1;j>0;j--)
			if(ans2[j+i]>j&&j+1>maxx)
			{
				maxx=j+1;
				break;
			} 
	printf("%d",maxx);
	return 0;
}

求各位奆佬帮助

2024/3/22 13:13
加载中...