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 "paint.h"
#include <bits/stdc++.h>
using namespace std;
int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
vector<vector<int>> colors(K);
for (int i = 0; i < M; i++) {
for (auto c : B[i]) {
colors[c].emplace_back(i);
}
}
int mx = 0;
vector<bool> possible(N + 1, false);
auto Update = [&](int i, int sgn) {
static vector<int> freq(M, 0);
static vector<int> cnt(M + 1, 0);
auto UpdateMax = [&]() {
while (mx + 1 <= M && cnt[mx + 1] > 0) mx++;
while (mx > 0 && cnt[mx] == 0) mx--;
};
for (auto j : colors[C[i]]) {
int v = j - (i % M);
if (v < 0) v += M;
if (sgn == 1) {
freq[v] += sgn;
cnt[freq[v]] += sgn;
} else if (sgn == -1) {
cnt[freq[v]] += sgn;
freq[v] += sgn;
}
UpdateMax();
}
};
for (int i = 0; i < M; i++) {
Update(i, +1);
}
if (mx == M) {
possible[0] = true;
}
for (int i = 0; i + M < N; i++) {
Update(i, -1);
Update(i + M, +1);
if (mx == M) {
possible[i + 1] = true;
}
}
deque<pair<int, int>> q;
vector<int> dist(N + 1, -1);
q.emplace_front(0, 0);
dist[0] = 0;
while (!q.empty()) {
int u = q.front().first;
int d = q.front().second;
q.pop_front();
if (dist[u] != d) {
continue;
}
if (possible[u] && (dist[u + M] == -1 || dist[u + M] > dist[u] + 1)) {
dist[u + M] = dist[u] + 1;
q.emplace_back(u + M, dist[u + M]);
}
if (u > 0 && (dist[u - 1] == -1 || dist[u - 1] > dist[u])) {
dist[u - 1] = dist[u];
q.emplace_front(u - 1, dist[u - 1]);
}
}
return dist[N];
}
# | 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... |