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>
using namespace std;
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);
}
auto ok = [&](int x) -> bool {
for (int med = 0; med < n; med++) {
if (mp[med].empty()) continue;
vector<int> cnt(n + 1, 0);
vector<int> le_lr(n + 1, 0), mx_le_lr(n + 1, 0);
vector<int> le_rl(n + 1, 0), mx_le_rl(n + 1, 0);
vector<int> ge_lr(n + 1, 0), mx_ge_lr(n + 1, 0);
vector<int> ge_rl(n + 1, 0), mx_ge_rl(n + 1, 0);
for (int i = 0; i < n; i++) {
cnt[i + 1] = cnt[i] + (a[i] <= med);
le_lr[i + 1] = le_lr[i] + 2 * (a[i] <= med) - 1;
ge_lr[i + 1] = ge_lr[i] + 2 * (a[i] >= med) - 1;
}
for (int i = n - 1; i >= 0; i--) {
le_rl[i] = le_rl[i + 1] + 2 * (a[i] <= med) - 1;
ge_rl[i] = ge_rl[i + 1] + 2 * (a[i] >= med) - 1;
}
mx_le_lr[n] = le_lr[n], mx_ge_lr[n] = ge_lr[n];
for (int i = n - 1; i >= 0; i--) {
mx_le_lr[i] = max(le_lr[i], mx_le_lr[i + 1]);
mx_ge_lr[i] = max(ge_lr[i], mx_ge_lr[i + 1]);
}
mx_le_rl[0] = le_rl[0], mx_ge_rl[0] = ge_rl[0];
for (int i = 1; i <= n; i++) {
mx_le_rl[i] = max(le_rl[i], mx_le_rl[i - 1]);
mx_ge_rl[i] = max(ge_rl[i], mx_ge_rl[i - 1]);
}
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 = cnt[r] - cnt[l];
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 = mx_le_lr[r] - le_lr[r] + mx_le_rl[l] - le_rl[l];
if (val + up >= 0) return true;
} else {
int val = 2 * gem - r + l; // < 0
int up = mx_ge_lr[r] - ge_lr[r] + mx_ge_rl[l] - ge_rl[l];
if (val + up >= 0) return true;
}
}
}
return false;
};
int ans = [&]() -> int {
int l = 1, r = n;
while (l < r) {
int m = (l + r + 1) >> 1;
if (ok(m)) {
l = m;
} else {
r = m - 1;
}
}
return l;
}();
return ans;
}
Compilation message (stderr)
sequence.cpp: In lambda function:
sequence.cpp:42:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int ii = 0; ii + x <= mp[med].size(); ii++) {
| ~~~~~~~^~~~~~~~~~~~~~~~~
# | 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... |