Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
int pos[500001][2];
int neg[500001][2];
int main() {
	int n, ans, a;
	cin >> n;
	ans = 1;
	for (int i = 0; i < 500001; i++) {
		pos[i][0] = pos[i][1] = -1;
		neg[i][0] = neg[i][1] = -1;
	}
	for (int i = 0; i < n; i++) {
		cin >> a;
		if (a >= 0) {
			if (pos[a][0] == -1)pos[a][0] = pos[a][1] = i;
			else pos[a][1] = i;
		}
		else if (a < 0) {
			if (neg[abs(a)][0] == -1)neg[abs(a)][0] = neg[abs(a)][1] = i;
			else neg[abs(a)][1] = i;
		}
	}
	for (int i = 0; i < 500001; i++) {
		ans = max(ans, pos[i][1] - pos[i][0] + 1);
		ans = max(ans, neg[i][1] - neg[i][0] + 1);
	}
	
	cout << ans;
	return 0;
}




Copy
Good Segment Sarah
GNU G++17
516 ms
8.1 MB
Accepted