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 "sequence.h"
#include <bits/stdc++.h>
#ifdef LOCAL
#include "template/dbgnc.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif
struct Tag {
int add = 0;
void apply(const Tag& t) {
add += t.add;
}
};
struct Info {
int mn = 0, mx = 0;
void apply(const Tag& t) {
mn += t.add, mx += t.add;
}
};
Info operator+(const Info& a, const Info& b) {
return {std::min(a.mn, b.mn), std::max(a.mx, b.mx)};
}
struct SegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
SegmentTree(int _n) : n(_n), info(4 * n), tag(4 * n) {}
void apply(int k, const Tag& t) {
info[k].apply(t);
tag[k].apply(t);
}
void push(int k) {
apply(k * 2, tag[k]);
apply(k * 2 + 1, tag[k]);
tag[k] = Tag();
}
void update(int k, int l, int r, int ql, int qr, const Tag& t) {
if (l > qr || r < ql) return;
if (ql <= l && r <= qr) {
info[k].apply(t);
tag[k].apply(t);
return;
}
push(k);
int mid = (l + r) / 2;
update(k * 2, l, mid, ql, qr, t);
update(k * 2 + 1, mid + 1, r, ql, qr, t);
info[k] = info[k * 2] + info[k * 2 + 1];
}
Info query(int k, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return {(int) 1e9, (int) -1e9};
if (ql <= l && r <= qr) {
return info[k];
}
push(k);
int mid = (l + r) / 2;
return query(k * 2, l, mid, ql, qr) + query(k * 2 + 1, mid + 1, r, ql, qr);
}
void update(int l, int r, const Tag& t) {
update(1, 0, n - 1, l, r, t);
}
Info query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
};
int sequence(int N, std::vector<int> A) {
std::vector<std::vector<int>> pos(N);
for (int i = 0; i < N; i++) {
pos[--A[i]].push_back(i);
}
auto check = [&](int mid) {
SegmentTree tree(N);
for (int i = 0; i < N; i++) {
tree.update(i, N - 1, {1});
}
for (int v = 0; v < N; v++) {
for (int i : pos[v]) {
tree.update(i, N - 1, {-1});
}
int sz = pos[v].size();
for (int i = 0; i + mid <= sz; i++) {
int l = pos[v][i];
int r = pos[v][i + mid - 1];
auto rl = tree.query(0, l);
int l1 = rl.mn, r1 = rl.mx;
auto rr = tree.query(r, N - 1);
int l2 = rr.mn, r2 = rr.mx;
if (std::max(l1, l2) <= std::min(r1, r2)) return true;
int v = std::min(std::abs(r1 - l2), std::abs(r2 - l1));
if (v <= mid) return true;
}
for (int i : pos[v]) {
tree.update(i, N - 1, {-1}); // becomes -1
}
}
return false;
};
int lb = 1, rb = N;
while (lb < rb) {
int mid = (lb + rb + 1) / 2;
if (check(mid)) {
lb = mid;
} else {
rb = mid - 1;
}
}
return lb;
}
#ifdef LOCAL
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &A[i]));
}
int result = sequence(N, A);
printf("%d\n", result);
return 0;
}
#endif
# | 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... |