90分求助 悬赏关注
查看原帖
90分求助 悬赏关注
429491
xin20110426楼主2023/11/15 08:39
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[100010], ans, Min[100010], Max[100010];
stack<int> s1;
stack<int> s2;
signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		while (!s1.empty() && a[i] >= a[s1.top()]) s1.pop();
		while (!s2.empty() && a[i] <= a[s2.top()]) s2.pop();
		if (!s1.empty()) Max[i] = s1.top();
		if (!s2.empty()) Min[i] = s2.top();
		s1.push(i);
		s2.push(i);
//		cout << Max[i] << " " << Min[i] << endl;
	}
	for (int i = n; i; ) {
		int j;
		for (j = i; Max[i] < Min[j]; j = Min[j]);
		ans = max(ans, i - j + 1);
		i = j - 1;
	}
	cout << ans;
	return 0;
}

2023/11/15 08:39
加载中...