This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;
template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;}
template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;}
#define files(FILENAME) read(FILENAME); write(FILENAME)
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define all(c) (c).begin(), (c).end()
#define sz(c) (int)(c).size()
#define left left228
#define right right228
#define y1 y1228
#define mp make_pair
#define pb push_back
#define y2 y2228
#define rank rank228
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 500228;
vector<int> pos[MAXN];
pair<int, int> tree[4 * MAXN];
int lazy[4 * MAXN];
pair<int, int> join(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 tl, int tr) {
if (tl < tr) {
lazy[2 * v] += lazy[v];
lazy[2 * v + 1] += lazy[v];
}
tree[v].first += lazy[v];
tree[v].second += lazy[v];
lazy[v] = 0;
}
void add(int v, int tl, int tr, int l, int r, int x) {
push(v, tl, tr);
if (l <= tl && tr <= r) {
lazy[v] += x;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
if (l <= tm) {
add(2 * v, tl, tm, l, min(tm, r), x);
}
if (tm + 1 <= r) {
add(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, x);
}
push(2 * v, tl, tm);
push(2 * v + 1, tm + 1, tr);
tree[v] = join(tree[2 * v], tree[2 * v + 1]);
}
pair<int, int> get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (l <= tl && tr <= r) {
return tree[v];
}
int tm = (tl + tr) / 2;
if (l <= tm && r <= tm) {
return get(2 * v, tl, tm, l, r);
} else if (tm + 1 <= l && tm + 1 <= r) {
return get(2 * v + 1, tm + 1, tr, l, r);
} else {
return join(get(2 * v, tl, tm, l, tm), get(2 * v + 1, tm + 1, tr, tm + 1, r));
}
}
int sequence(int n, vector<int> a) {
for (int i = 0; i < n; i++) {
a[i]--;
pos[a[i]].pb(i);
}
for (int i = 0; i < n; i++) {
add(1, 0, n, i, n, 1);
}
int ret = 1;
vector<int> pref_min1(n), pref_min2(n), pref_max1(n), pref_max2(n), suff_min1(n), suff_min2(n), suff_max1(n), suff_max2(n);
for (int val = 0; val < n; val++) {
for (int i = 0; i < sz(pos[val]); i++) {
pair<int, int> res = get(1, 0, n, 0, pos[val][i]);
pref_min1[i] = res.first;
pref_max1[i] = res.second;
res = get(1, 0, n, pos[val][i] + 1, n);
suff_min1[i] = res.first;
suff_max1[i] = res.second;
}
for (auto p: pos[val]) {
add(1, 0, n, p, n, -2);
}
for (int i = 0; i < sz(pos[val]); i++) {
pair<int, int> res = get(1, 0, n, 0, pos[val][i]);
pref_min2[i] = res.first;
pref_max2[i] = res.second;
res = get(1, 0, n, pos[val][i] + 1, n);
suff_min2[i] = res.first;
suff_max2[i] = res.second;
}
int l = 1, r = sz(pos[val]), sol = 0;
while (l <= r) {
int m = (l + r) / 2;
bool ok = false;
for (int i = 0; i + m - 1 < sz(pos[val]); i++) {
if (pref_min1[i] <= suff_max1[i + m - 1] && suff_min1[i + m - 1] <= pref_max1[i]) {
ok = true;
} else if (pref_min2[i] <= suff_max2[i + m - 1] && suff_min2[i + m - 1] <= pref_max2[i]) {
ok = true;
} else if (pref_max1[i] <= suff_min1[i + m - 1] && suff_max2[i + m - 1] <= pref_min2[i]) {
ok = true;
} else if (suff_max1[i] <= pref_min1[i + m - 1] && pref_max2[i + m - 1] <= suff_min2[i]) {
ok = true;
}
}
if (ok == true) {
l = m + 1;
sol = m;
} else {
r = m - 1;
}
}
chkmax(ret, sol);
}
return ret;
}
/**
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//read(FILENAME);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << sequence(n, a) << '\n';
return 0;
}
**/
# | 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... |