#include "paint.h"
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int INF = 1e9;
struct SegTree {
int n,N;
vector<int> tr;
SegTree(int sz):n(sz){
N = (1<<(32-__builtin_clz(n)));
tr.assign(2*N,INF);
}
void upd(int idx, int val){
for(idx += N, tr[idx] = val, idx >>= 1; idx > 0; idx >>= 1) tr[idx] = min(tr[idx<<1],tr[idx<<1|1]);
}
int get(int l, int r){
int ans = INF;
for(l += N, r += N; l < r; l >>= 1, r >>= 1){
if(l&1) ans = min(tr[l++],ans);
if(r&1) ans = min(ans,tr[--r]);
}
return ans;
}
};
int minimumInstructions(int N, int M, int K, std::vector<int> C, std::vector<int> A, std::vector<std::vector<int>> B) {
// [y,y+M-1]
vector<set<int>> BB;
for(auto z: B) {
BB.emplace_back(set<int>(z.begin(),z.end()));
}
// M = 3
// [1 0 1]
// [1 1 1]
// [1 1 0]
// [0 0 1] N-M = 3
// [1 1 0]
// [0 1 1]
vector<vector<int>> dp1(N, vector<int>(M,0));
vector<pair<int,int>> segs;
for(int y = N-1; y >= 0; --y){
for(int x = 0; x < M; ++x){
if(BB[x].count(C[y])) {
if(y == N-1) dp1[y][x] = 1;
else dp1[y][x] = dp1[y+1][(x+1)%M]+1;
}
if(y <= N-M && dp1[y][x] >= M) {
segs.emplace_back(y,y+M-1);
break;
}
}
}
sort(segs.begin(),segs.end());
SegTree sg(N);
vector<int> dp(N,INF);
for(auto z: segs){
int l = z.first, r = z.second;
if(l == 0) dp[r] = 1;
else dp[r] = min(sg.get(l-1,r)+1,dp[r]);
sg.upd(r,dp[r]);
}
if(dp[N-1] == INF) return -1;
else return dp[N-1];
};
// O(N*M*logC+N*logN)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
788 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
25 ms |
6204 KB |
Output is correct |
23 |
Correct |
25 ms |
6180 KB |
Output is correct |
24 |
Correct |
26 ms |
6156 KB |
Output is correct |
25 |
Correct |
5 ms |
2008 KB |
Output is correct |
26 |
Correct |
5 ms |
2008 KB |
Output is correct |
27 |
Correct |
4 ms |
2004 KB |
Output is correct |
28 |
Correct |
4 ms |
1876 KB |
Output is correct |
29 |
Correct |
4 ms |
1876 KB |
Output is correct |
30 |
Correct |
4 ms |
1876 KB |
Output is correct |
31 |
Correct |
419 ms |
159644 KB |
Output is correct |
32 |
Correct |
423 ms |
159608 KB |
Output is correct |
33 |
Correct |
423 ms |
159560 KB |
Output is correct |
34 |
Correct |
420 ms |
159600 KB |
Output is correct |
35 |
Correct |
428 ms |
159708 KB |
Output is correct |
36 |
Correct |
444 ms |
159604 KB |
Output is correct |
37 |
Execution timed out |
1592 ms |
163452 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
288 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
2 ms |
788 KB |
Output is correct |
20 |
Correct |
2 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
1 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
2 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
516 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
788 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
25 ms |
6204 KB |
Output is correct |
25 |
Correct |
25 ms |
6180 KB |
Output is correct |
26 |
Correct |
26 ms |
6156 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
596 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
516 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
788 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
25 ms |
6204 KB |
Output is correct |
25 |
Correct |
25 ms |
6180 KB |
Output is correct |
26 |
Correct |
26 ms |
6156 KB |
Output is correct |
27 |
Correct |
5 ms |
2008 KB |
Output is correct |
28 |
Correct |
5 ms |
2008 KB |
Output is correct |
29 |
Correct |
4 ms |
2004 KB |
Output is correct |
30 |
Correct |
4 ms |
1876 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
419 ms |
159644 KB |
Output is correct |
34 |
Correct |
423 ms |
159608 KB |
Output is correct |
35 |
Correct |
423 ms |
159560 KB |
Output is correct |
36 |
Correct |
420 ms |
159600 KB |
Output is correct |
37 |
Correct |
428 ms |
159708 KB |
Output is correct |
38 |
Correct |
444 ms |
159604 KB |
Output is correct |
39 |
Execution timed out |
1592 ms |
163452 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
724 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
852 KB |
Output is correct |
21 |
Correct |
2 ms |
788 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
25 ms |
6204 KB |
Output is correct |
25 |
Correct |
25 ms |
6180 KB |
Output is correct |
26 |
Correct |
26 ms |
6156 KB |
Output is correct |
27 |
Correct |
5 ms |
2008 KB |
Output is correct |
28 |
Correct |
5 ms |
2008 KB |
Output is correct |
29 |
Correct |
4 ms |
2004 KB |
Output is correct |
30 |
Correct |
4 ms |
1876 KB |
Output is correct |
31 |
Correct |
4 ms |
1876 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
419 ms |
159644 KB |
Output is correct |
34 |
Correct |
423 ms |
159608 KB |
Output is correct |
35 |
Correct |
423 ms |
159560 KB |
Output is correct |
36 |
Correct |
420 ms |
159600 KB |
Output is correct |
37 |
Correct |
428 ms |
159708 KB |
Output is correct |
38 |
Correct |
444 ms |
159604 KB |
Output is correct |
39 |
Execution timed out |
1592 ms |
163452 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |