# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163417 | iefnah06 | Rope (JOI17_rope) | C++11 | 1157 ms | 80760 KiB |
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 <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const int MAXM = MAXN;
int N, M;
int A[MAXN];
vector<int> v[MAXM];
int cost[MAXM];
int ind[MAXM];
int main() {
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]); A[i]--;
v[A[i]].push_back(i);
cost[A[i]] -= 1;
}
iota(ind, ind + M, 0);
sort(ind, ind + M, [&](int a, int b) { return cost[a] < cost[b]; });
for (int c = 0; c < M; c++) {
int ans = N - int(v[c].size());
for (int par = 0; par < 2; par++) {
// at most 2 * |v[c]| incident colors
int cnt = 0;
for (int i : v[c]) {
if (i > 0 && i % 2 != par) {
cost[A[i - 1]] += 1;
cnt++;
}
if (i < N - 1 && (i + 1) % 2 != par) {
cost[A[i + 1]] += 1;
cnt++;
}
}
cnt = min(cnt + 2, M);
int extra = N;
for (int i = 0; i < cnt; i++) {
if (ind[i] == c) continue;
extra = min(extra, cost[ind[i]]);
}
ans = min(ans, N - int(v[c].size()) + extra);
for (int i : v[c]) {
if (i > 0 && i % 2 != par) {
cost[A[i - 1]]--;
}
if (i < N - 1 && (i + 1) % 2 != par) {
cost[A[i + 1]]--;
}
}
}
printf("%d\n", ans);
}
return 0;
}
Compilation message (stderr)
# | 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... |