You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
770 B
C++
33 lines
770 B
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
|
|
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 3e5 + 50;
|
|
int a[N];
|
|
int main()
|
|
{
|
|
int n;
|
|
scanf("%d", &n);
|
|
for (int i = 0; i < n; i++)
|
|
scanf("%d", a + i);
|
|
map<int, pair<int, int>> M;
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
if (M.count(a[i]))
|
|
M[a[i]].second = i;
|
|
else
|
|
M[a[i]] = make_pair(i, i);
|
|
}
|
|
int ans = 0;
|
|
set<int> S;
|
|
for (int i = 0; i < n; i++) S.insert(M[a[i]].second);
|
|
for (int i = 0; i < n; i++)
|
|
{
|
|
S.erase(M[a[i]].second);
|
|
ans = max(ans, *S.rbegin() - i);
|
|
S.insert(M[a[i]].second);
|
|
}
|
|
printf("%d", ans);
|
|
return 0;
|
|
}
|