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>
#include "paint.h"
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int inf = 1e9 + 7;
int minimumInstructions(
int N, int M, int K, std::vector<int> C,
std::vector<int> A, std::vector<std::vector<int>> B) {
for (int i = 0;i < M;++i)
sort(AI(B[i]));
vector<vector<int>> pos(K);
for (int i = 0;i < M;++i) {
for (int c : B[i]) pos[c].pb(i);
}
vector<int> cnt_ok(N+1);
auto bad = [&](int i, int j) {
return !binary_search(AI(B[j%M]), C[i]);
};
vector<int> col_cnt(M);
int gcnt = 0;
auto mm = [&](int d) {
d %= M;
if (d < 0) d += M;
return d;
};
auto addin = [&](int v, int d) {
v = mm(v);
if (d == 1) {
gcnt += ++col_cnt[v] == M;
}
else {
gcnt -= col_cnt[v]-- == M;
}
};
auto putin = [&](int ind, int d) {
for (int j : pos[ C[ind] ]) {
addin(ind - j, d);
}
};
for (int i = 0;i < M;++i)
putin(i, 1);
for (int i = 0;i <= N-M;++i) {
cnt_ok[i] = gcnt ? M : 0;
if (i == N-M) break;
putin(i, -1);
putin(i+M, 1);
}
// good is the max index I can take
int res = 0, good = 0, bst = -1;
cnt_ok[N] = M;
for (int i = 0;i <= N;++i) {
if (cnt_ok[i] < M) continue;
while (i > good) {
if (bst == -1) return -1;
++res;
good = bst + M;
bst = -1;
}
chmax(bst, i);
}
return res;
}
Compilation message (stderr)
paint.cpp: In function 'int minimumInstructions(int, int, int, std::vector<int>, std::vector<int>, std::vector<std::vector<int> >)':
paint.cpp:34:7: warning: variable 'bad' set but not used [-Wunused-but-set-variable]
34 | auto bad = [&](int i, int j) {
| ^~~| # | 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... |