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 <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
#include "sequence.h"
using namespace std;
mt19937 rnd(7069);
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const int N = 500005;
int a[N], p[N], pref[N], t[N * 4];
vi g[5];
int n, m, ans = 1;
void bld(int l = 1, int r = m, int pos = 1) {
if (l == r) {
p[l] = pos;
return;
}
int mid = (l + r) / 2;
bld(l, mid, 2 * pos);
bld(mid + 1, r, 2 * pos + 1);
}
int qry(int l, int r, int tl = 1, int tr = m, int pos = 1) {
if (l > r) return MAX;
if (l == tl && r == tr) return t[pos];
int mid = (tl + tr) / 2;
if (mid >= r) {
return qry(l, r, tl, mid, 2 * pos);
}
if (mid < l) {
return qry(l, r, mid + 1, tr, 2 * pos + 1);
}
return min(qry(l, mid, tl, mid, 2 * pos), qry(mid + 1, r, mid + 1, tr, 2 * pos + 1));
}
void upd(int pos, int val) {
pos = p[pos];
t[pos] = val;
pos /= 2;
while (pos) {
t[pos] = min(t[2 * pos], t[2 * pos + 1]);
pos /= 2;
}
}
void solve_1(int type) {
for (int i = 1; i <= m; ++i) {
int val = pref[i], mx = val + 1;
if (type == 1 && i % 2) ++mx;
else if (type == 2 && !(i % 2)) ++mx;
if (qry(i + 1, m) > mx) continue;
int l = i + 1, r = m, mid, ind = i;
while (l < r) {
mid = (l + r) / 2;
if (qry(mid + 1, r) <= mx) {
l = ind = mid + 1;
}
else if (qry(l, mid) <= mx) {
r = ind = mid;
}
else {
break;
}
}
ans = max(ans, ind - i + 1);
}
}
int sequence(int N, std::vector<int> A) {
n = N;
g[1] = { 0 };
g[2] = { 0 };
g[3] = { 0 };
for (int i = 1; i <= n; ++i) {
a[i] = A[i - 1];
g[a[i]].pub(i);
pref[i] = pref[i - 1];
if (a[i] > 1) ++pref[i];
else --pref[i];
}
m = (int)g[1].size() - 1;
bld();
for (int i = 1; i <= m; ++i) {
if (i % 2) upd(i, MAX);
else upd(i, pref[g[1][i]]);
}
solve_1(1);
for (int i = 1; i <= m; ++i) {
if (i % 2) upd(i, pref[g[1][i]]);
else upd(i, MAX);
}
solve_1(2);
swap(g[1], g[3]);
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1];
if (a[i] < 3) ++pref[i];
else --pref[i];
}
m = (int)g[1].size() - 1;
bld();
for (int i = 1; i <= m; ++i) {
if (i % 2) upd(i, MAX);
else upd(i, pref[g[1][i]]);
}
solve_1(1);
for (int i = 1; i <= m; ++i) {
if (i % 2) upd(i, pref[g[1][i]]);
else upd(i, MAX);
}
solve_1(2);
return ans;
}
/*
7
3 2 1 3 2 3 1
*/
# | 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... |