This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#include <fstream>
#define fastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 500005;
int n, a[N], ans = 1;
vector<int> pos[N];
pair<int, int> t[N * 4];
pair<int, int> t2[N * 4];
int d[N * 4];
int d2[N * 4];
pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
return MP(min(a.first, b.first), max(a.second, b.second));
}
void push(int v, int l, int r)
{
if (d[v] == 0)
return;
if (l != r)
{
d[v * 2] += d[v];
d[v * 2 + 1] += d[v];
}
t[v] = MP(t[v].first + d[v], t[v].second + d[v]);
d[v] = 0;
}
void add(int v, int l, int r, int i, int j, int val)
{
push(v, l, r);
if (i > j)
return;
if (i == l && j == r)
{
d[v] += val;
push(v, l, r);
return;
}
int m = (l + r) / 2;
add(v * 2, l, m, i, min(m, j), val);
add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
pair<int, int> get(int v, int l, int r, int i, int j)
{
push(v, l, r);
if (i > j)
return MP(mod, -mod);
if (i == l && j == r)
return t[v];
int m = (l + r) / 2;
return merge(
get(v * 2, l, m, i, min(m, j)),
get(v * 2 + 1, m + 1, r, max(m + 1, i), j)
);
}
void push2(int v, int l, int r)
{
if (d2[v] == 0)
return;
if (l != r)
{
d2[v * 2] += d2[v];
d2[v * 2 + 1] += d2[v];
}
t2[v] = MP(t2[v].first + d2[v], t2[v].second + d2[v]);
d2[v] = 0;
}
void add2(int v, int l, int r, int i, int j, int val)
{
push2(v, l, r);
if (i > j)
return;
if (i == l && j == r)
{
d2[v] += val;
push2(v, l, r);
return;
}
int m = (l + r) / 2;
add2(v * 2, l, m, i, min(m, j), val);
add2(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
t2[v] = merge(t2[v * 2], t2[v * 2 + 1]);
}
pair<int, int> get2(int v, int l, int r, int i, int j)
{
push2(v, l, r);
if (i > j)
return MP(mod, -mod);
if (i == l && j == r)
return t2[v];
int m = (l + r) / 2;
return merge(
get2(v * 2, l, m, i, min(m, j)),
get2(v * 2 + 1, m + 1, r, max(m + 1, i), j)
);
}
bool check(int x, int k, int p)
{
int i = pos[x][p];
int j = pos[x][k];
int len = p - k + 1;
pair<int, int> dr = get(1, 0, n, i, n);
dr.first -= len;
dr.second -= len;
pair<int, int> dl = get(1, 0, n, 0, j - 1);
pair<int, int> d = MP(dr.first - dl.second, dr.second - dl.first);
pair<int, int> dr2 = get2(1, 0, n, i, n);
dr2.first += len;
dr2.second += len;
pair<int, int> dl2 = get2(1, 0, n, 0, j - 1);
pair<int, int> d2 = MP(dr2.first - dl2.second, dr2.second - dl2.first);
return ((d.second < 0 && len >= -d.second) || (d.first > 0 && len >= d.first) || (d.first <= 0 && d.second >= 0) || (d2.second < 0 && len >= -d2.second) || (d2.first > 0 && len >= d2.first) || (d2.first <= 0 && d2.second >= 0));
}
bool check(int x, int len)
{
for (int p = len - 1; p < pos[x].size(); p++)
if (check(x, p - len + 1, p))
return true;
return false;
}
int sequence(int nn, vector<int> aa)
{
n = nn;
for (int i = 1; i <= n; i++)
a[i] = aa[i - 1];
for (int i = 1; i <= n; i++)
pos[a[i]].push_back(i);
for (int i = 1; i <= n; i++)
{
add(1, 0, n, i, n, 1);
add2(1, 0, n, i, n, 1);
}
for (int x = 1; x <= n; x++)
{
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
add2(1, 0, n, i, n, -2);
}
int l = ans, r = pos[x].size();
while (l <= r)
{
int m = (l + r) / 2;
if (check(x, m))
{
ans = max(ans, m);
l = m + 1;
}
else
r = m - 1;
}
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
add(1, 0, n, i, n, -2);
}
}
return ans;
}
#ifdef death
void solve()
{
int nn;
vector<int> aa;
cin >> nn;
for (int i = 0; i < nn; i++)
{
int x;
cin >> x;
aa.push_back(x);
}
cout << sequence(nn, aa) << endl;
}
int main()
{
fastIO;
int T = 1;
//cin >> T;
while (T--)
solve();
return 0;
}
#endif // death
/*
7
1 2 3 1 2 1 3
9
1 1 2 3 4 3 2 1 1
14
2 6 2 5 3 4 2 1 4 3 5 6 3 2
*/
Compilation message (stderr)
sequence.cpp: In function 'bool check(int, int)':
sequence.cpp:164:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
164 | for (int p = len - 1; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:190:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
190 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:210:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
210 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |