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;
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) "MJ >> LAMELO"
#endif
int minimumInstructions(
int N, int M, int K, std::vector<int> C,
std::vector<int> A, std::vector<std::vector<int>> B) {
vector<vector<int>> g(K);
for (int i = 0; i < M; i++) {
for (int j : B[i]) {
g[j].push_back(i);
}
}
int cnt_m = 0;
vector<int> cnt(M);
for (int i = 0; i < M; i++) {
for (int j : g[C[i]]) {
int id = (j - i + M) % M;
cnt[id]++;
if (cnt[id] == M) {
cnt_m++;
}
}
}
if (!cnt_m) {
return -1;
}
int ans = 1;
int lst = M - 1;
int best = M - 1;
for (int i = 1; i + M - 1 < N; i++) {
for (int j : g[C[i - 1]]) {
if (cnt[j] == M) {
cnt_m--;
}
cnt[j]--;
}
for (int j : g[C[i + M - 1]]) {
cnt[(j + 1) % M]++;
if (cnt[(j + 1) % M] == M) {
cnt_m++;
}
}
if (cnt_m) {
best = i + M - 1;
}
if (i == lst + 1) {
if (best < i) {
return -1;
}
ans++;
lst = best;
}
}
if (best < N - 1) {
return -1;
}
if (lst < best) {
ans++;
}
return ans;
}
# | 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... |