#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
#include "paint.h"
int minimumInstructions(int N, int M, int K, vi C, vi A, vector<vi> B) {
vi use(K, -1); // use[i]: contractor who paints color i
for (int i = 0; i < M; i++) {
for (int j = 0; j < A[i]; j++) {
use[B[i][j]] = i;
}
}
/* cout<<"use: ";
for (int x : use)
cout<<x<<" ";
cout<<endl;*/
vi valr(N, -1);
for (int i = 0; i < N; i++) {
valr[i] = use[C[i]];
}
vi pfst(N + 1, 0); // prefix sum of number of not -1's
for (int i = 1; i <= N; i++) {
pfst[i] = pfst[i - 1];
if (valr[i - 1] != -1)
pfst[i]++;
}
/* cout<<"valr: ";
for (int x : valr)
cout<<x<<" ";
cout<<endl;
cout<<"pfst: ";
for (int x : pfst)
cout<<x<<" ";
cout<<endl;*/
vi diff(N - 1, 0);
vi is1(N - 1, 0); // 1 if diff[i] is 1; 0 otherwise
for (int i = 0; i < N - 1; i++) {
if (valr[i] != -1 && valr[i + 1] != -1) {
diff[i] = valr[i + 1] - valr[i];
diff[i] %= M;
diff[i] += M;
diff[i] %= M;
if (diff[i] == 1 || M == 1)
is1[i] = 1;
}
}
/* cout<<"diff: ";
for (int x : diff)
cout<<x<<" ";
cout<<endl;*/
vi pfs(N, 0);
pfs[0] = 0;
for (int i = 1; i < N; i++) {
pfs[i] = pfs[i - 1] + is1[i - 1];
}
/* cout<<"pfs: ";
for (int x : pfs)
cout<<x<<" ";
cout<<endl;*/
vector<bool> cfill(N - M + 1, false);
for (int i = 0; i < N - M + 1; i++) {
if (pfst[i + M] - pfst[i] == M && pfs[i + M - 1] - pfs[i] == M - 1)
cfill[i] = true;
}
/* cout<<"cfill: ";
for (bool b : cfill)
cout<<b<<" ";
cout<<endl;*/
set<int> good;
for (int i = 0; i < N - M + 1; i++) {
if (cfill[i])
good.insert(i);
}
good.insert(-M);
good.insert(N);
int curr = -M;
int total = 0;
while (curr < N) {
auto it = good.lower_bound(curr + M + 1);
if (it == good.begin())
return -1;
int newc = *(--it);
if (newc == curr)
return -1;
curr = newc;
total++;
}
total--;
return total;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
644 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
7 ms |
1588 KB |
Output is correct |
23 |
Correct |
8 ms |
1628 KB |
Output is correct |
24 |
Correct |
7 ms |
1628 KB |
Output is correct |
25 |
Correct |
5 ms |
1884 KB |
Output is correct |
26 |
Correct |
5 ms |
1884 KB |
Output is correct |
27 |
Correct |
5 ms |
1372 KB |
Output is correct |
28 |
Correct |
2 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
5 ms |
2396 KB |
Output is correct |
32 |
Correct |
5 ms |
2396 KB |
Output is correct |
33 |
Correct |
6 ms |
2396 KB |
Output is correct |
34 |
Correct |
6 ms |
2396 KB |
Output is correct |
35 |
Correct |
6 ms |
2308 KB |
Output is correct |
36 |
Correct |
6 ms |
2396 KB |
Output is correct |
37 |
Correct |
8 ms |
2140 KB |
Output is correct |
38 |
Correct |
9 ms |
2172 KB |
Output is correct |
39 |
Correct |
9 ms |
2140 KB |
Output is correct |
40 |
Correct |
26 ms |
7004 KB |
Output is correct |
41 |
Correct |
15 ms |
4444 KB |
Output is correct |
42 |
Correct |
16 ms |
5468 KB |
Output is correct |
43 |
Correct |
5 ms |
2396 KB |
Output is correct |
44 |
Correct |
5 ms |
2140 KB |
Output is correct |
45 |
Correct |
7 ms |
3164 KB |
Output is correct |
46 |
Correct |
25 ms |
10636 KB |
Output is correct |
47 |
Correct |
16 ms |
7256 KB |
Output is correct |
48 |
Correct |
18 ms |
8784 KB |
Output is correct |
49 |
Correct |
17 ms |
7836 KB |
Output is correct |
50 |
Correct |
17 ms |
7516 KB |
Output is correct |
51 |
Correct |
15 ms |
5724 KB |
Output is correct |
52 |
Correct |
27 ms |
8096 KB |
Output is correct |
53 |
Correct |
27 ms |
7864 KB |
Output is correct |
54 |
Correct |
19 ms |
5724 KB |
Output is correct |
55 |
Correct |
9 ms |
3676 KB |
Output is correct |
56 |
Correct |
11 ms |
3676 KB |
Output is correct |
57 |
Correct |
7 ms |
3164 KB |
Output is correct |
58 |
Correct |
27 ms |
12832 KB |
Output is correct |
59 |
Correct |
29 ms |
12892 KB |
Output is correct |
60 |
Correct |
28 ms |
12884 KB |
Output is correct |
61 |
Correct |
29 ms |
12884 KB |
Output is correct |
62 |
Correct |
27 ms |
12888 KB |
Output is correct |
63 |
Correct |
30 ms |
13024 KB |
Output is correct |
64 |
Correct |
23 ms |
9808 KB |
Output is correct |
65 |
Correct |
23 ms |
9700 KB |
Output is correct |
66 |
Correct |
22 ms |
9704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
17 |
Correct |
1 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
644 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |