Source Code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <math.h>
#include <vector>
#include <set>
#include <map>
using namespace std;
int pos[100001][2];
int neg[100001][2];
int main() {
	int n, ans, a;
	cin >> n;
	ans = 1;
	for (int i = 0; i < 100001; 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 < 100001; 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
4 ms
1.9 MB
Runtime Error