#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
int add = 0;
void apply(const Tag &t) & {
add += t.add;
}
};
constexpr int INF = 1e9;
struct Info {
int mx = -INF;
void apply(const Tag &t) & {
mx += t.add;
}
Info operator+(const Info &b) {
return {max(mx, b.mx)};
}
};
struct Fenwick {
int n;
vector<int> bit;
Fenwick(int n) : n(n), bit(n + 1, 0) {}
void add(int i, int delta) {
for (i++; i <= n; i += i & -i) {
bit[i] += delta;
}
}
int sum(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
int sum(int ql, int qr) {
return sum(qr) - sum(ql);
}
};
int sequence(int n, vector<int> a) {
vector<vector<int>> mp(n);
for (int i = 0; i < n; i++) {
a[i]--;
mp[a[i]].push_back(i);
}
int mx = 0;
for (int x = 0; x < n; x++) {
mx = max(mx, (int)mp[x].size());
}
auto ok = [&](int x) -> bool {
Fenwick fen(n);
vector<Info> lr(n + 1), rl(n + 1);
for (int i = 0; i <= n; i++) {
lr[i] = {-i};
rl[i] = {i - n};
}
LazySegmentTree<Info, Tag> seg_le_lr(lr);
LazySegmentTree<Info, Tag> seg_le_rl(rl);
LazySegmentTree<Info, Tag> seg_ge_lr(lr);
LazySegmentTree<Info, Tag> seg_ge_rl(rl);
for (int i = 0; i < n; i++) {
seg_ge_lr.rangeApply(i + 1, n + 1, {+2});
seg_ge_rl.rangeApply(0, i + 1, {+2});
}
for (int med = 0; med < n; med++) {
if (mp[med].empty()) continue;
for (int i : mp[med]) {
fen.add(i, +1);
seg_le_lr.rangeApply(i + 1, n + 1, {+2});
seg_le_rl.rangeApply(0, i + 1, {+2});
}
for (int ii = 0; ii + x <= mp[med].size(); ii++) {
int l = mp[med][ii];
int r = mp[med][ii + x - 1] + 1;
int lem = fen.sum(l, r);
int gem = (r - l) - lem + x;
if (2 * lem >= r - l && 2 * gem >= r - l) return true;
if (2 * lem < r - l) {
int val = 2 * lem - r + l; // < 0
int up = seg_le_lr.rangeQuery(r, n + 1).mx - seg_le_lr.rangeQuery(r, r + 1).mx + seg_le_rl.rangeQuery(0, l + 1).mx - seg_le_rl.rangeQuery(l, l + 1).mx;
if (val + up >= 0) return true;
} else {
int val = 2 * gem - r + l; // < 0
int up = seg_ge_lr.rangeQuery(r, n + 1).mx - seg_ge_lr.rangeQuery(r, r + 1).mx + seg_ge_rl.rangeQuery(0, l + 1).mx - seg_ge_rl.rangeQuery(l, l + 1).mx;
if (val + up >= 0) return true;
}
}
for (int i : mp[med]) {
seg_ge_lr.rangeApply(i + 1, n + 1, {-2});
seg_ge_rl.rangeApply(0, i + 1, {-2});
}
}
return false;
};
int ans = [&]() -> int {
int l = 1, r = mx;
while (l < r) {
int m = (l + r + 1) >> 1;
if (ok(m)) {
l = m;
} else {
r = m - 1;
}
}
return l;
}();
return ans;
}
Compilation message
sequence.cpp: In lambda function:
sequence.cpp:225:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
225 | for (int ii = 0; ii + x <= mp[med].size(); ii++) {
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
660 KB |
Output is correct |
16 |
Correct |
9 ms |
660 KB |
Output is correct |
17 |
Correct |
12 ms |
660 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
700 KB |
Output is correct |
20 |
Correct |
11 ms |
692 KB |
Output is correct |
21 |
Correct |
14 ms |
708 KB |
Output is correct |
22 |
Correct |
13 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
821 ms |
68504 KB |
Output is correct |
3 |
Correct |
1723 ms |
70716 KB |
Output is correct |
4 |
Execution timed out |
2047 ms |
61480 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
2070 ms |
60968 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
970 ms |
74340 KB |
Output is correct |
2 |
Correct |
960 ms |
77708 KB |
Output is correct |
3 |
Correct |
414 ms |
76932 KB |
Output is correct |
4 |
Correct |
403 ms |
76928 KB |
Output is correct |
5 |
Correct |
629 ms |
73584 KB |
Output is correct |
6 |
Correct |
924 ms |
73608 KB |
Output is correct |
7 |
Correct |
860 ms |
72476 KB |
Output is correct |
8 |
Correct |
684 ms |
72064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
660 KB |
Output is correct |
16 |
Correct |
9 ms |
660 KB |
Output is correct |
17 |
Correct |
12 ms |
660 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
700 KB |
Output is correct |
20 |
Correct |
11 ms |
692 KB |
Output is correct |
21 |
Correct |
14 ms |
708 KB |
Output is correct |
22 |
Correct |
13 ms |
704 KB |
Output is correct |
23 |
Correct |
296 ms |
14336 KB |
Output is correct |
24 |
Correct |
280 ms |
14880 KB |
Output is correct |
25 |
Correct |
269 ms |
14648 KB |
Output is correct |
26 |
Correct |
709 ms |
13916 KB |
Output is correct |
27 |
Correct |
651 ms |
13880 KB |
Output is correct |
28 |
Correct |
731 ms |
13664 KB |
Output is correct |
29 |
Correct |
1012 ms |
13316 KB |
Output is correct |
30 |
Correct |
994 ms |
13316 KB |
Output is correct |
31 |
Correct |
1025 ms |
14096 KB |
Output is correct |
32 |
Correct |
10 ms |
5724 KB |
Output is correct |
33 |
Correct |
827 ms |
14940 KB |
Output is correct |
34 |
Correct |
1120 ms |
14596 KB |
Output is correct |
35 |
Correct |
792 ms |
14516 KB |
Output is correct |
36 |
Correct |
1347 ms |
14888 KB |
Output is correct |
37 |
Correct |
1309 ms |
14588 KB |
Output is correct |
38 |
Correct |
1375 ms |
14592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
856 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
660 KB |
Output is correct |
16 |
Correct |
9 ms |
660 KB |
Output is correct |
17 |
Correct |
12 ms |
660 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
700 KB |
Output is correct |
20 |
Correct |
11 ms |
692 KB |
Output is correct |
21 |
Correct |
14 ms |
708 KB |
Output is correct |
22 |
Correct |
13 ms |
704 KB |
Output is correct |
23 |
Correct |
821 ms |
68504 KB |
Output is correct |
24 |
Correct |
1723 ms |
70716 KB |
Output is correct |
25 |
Execution timed out |
2047 ms |
61480 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |