이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (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... |