Submission #979674

# Submission time Handle Problem Language Result Execution time Memory
979674 2024-05-11T09:42:33 Z vjudge1 Painting Walls (APIO20_paint) C++17
28 / 100
984 ms 327008 KB
#include <bits/stdc++.h>
#include "paint.h"

using namespace std;
using ll = long long;

const int N1 = 20200;
const int M1 = 2020;

int n, m;

int g[N1][M1];
int f[N1][M1];

void precalc() {
    for (int j = 0; j < m; j++) {
        int x = n - 1, y = j;
        while (g[x][y] && f[n - 1][j] < m) {
            f[n - 1][j]++;
            x = (x + 1) % n;
            y = (y + 1) % m;
        }
    }

    for (int i = n - 2; i >= 0; i--) {
        for (int j = 0; j < m; j++) {
            if (g[i][j]) f[i][j] = 1 + f[i + 1][(j + 1) % m];
            else f[i][j] = 0;
        }
    }
}

const int N = 100100;

int to[N][18];
int dt[N][18];

int minimumInstructions(
    int N, int M, int K, vector<int> C,
    vector<int> A, vector<vector<int>> B) {
    n = N,
    m = M;

    bool able[n] = {};

    if (n < N1 && m < M1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int x = lower_bound(B[j].begin(), B[j].end(), C[i]) - B[j].begin();
                g[i][j] = (x != A[j] && B[j][x] == C[i]);

            }
        }

        precalc();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) 
                able[i] |= (f[i][j] >= m);
        }
    } else {
        bool used[K + 1] = {};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < A[i]; j++) {
                used[B[i][j]] = true;
            }
        }
        int cnt = 0;
        map<int, int> mp;
        for (int i = 0; i < m; i++) {
            mp[C[i]]++;
            cnt += used[i];
        }

        for (int i = 0; i < n; i++) {
            able[i] = ((int)mp.size() == m && cnt == m);
            cnt -= used[C[i]];
            cnt += used[C[(i + m) % n]]; 
            mp[C[i]]--;
            mp[C[(i + m) % n]]++;
            if (!mp[C[i]]) mp.erase(C[i]);
        }
    }
    
    {
        int st = 0;
        while (st < n && !able[st]) st++;
        if (st == n) return -1;
        if (m == n)
            return 1;
        
        int nxt = 0, w = 0;
        for (int i = 0; i < n; i++) {
            if (able[(i + st) % n]) {
                nxt = (i + st + m) % n;
                w = 0;
            } 
            if (w >= m) nxt = (st + i) % n;
            to[(i + st) % n][0] = nxt;
            dt[(i + st) % n][0] = (nxt - st - i + 2 * n) % n;
            w++;
        }
    }
    for (int k = 1; k < 18; k++) {
        for (int i = 0; i < n; i++) {
            to[i][k] = to[to[i][k - 1]][k - 1];
            dt[i][k] = dt[i][k - 1] + dt[to[i][k - 1]][k - 1];
        }
    }
    
    int res = 1e9;
    for (int i = 0; i < n; i++) {
        int x = i;
        int ans = 0;
        int dist = 0;
        for (int k = 17; k >= 0; k--) {
            if (dist + dt[x][k] < n) {
                ans += (1 << k);
                dist += dt[x][k];
                x = to[x][k];
            }
        }
        if (dist + dt[x][0] >= n)
            res = min(res, ans + 1);
    }
    if (res == 1e9) return -1;
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 2 ms 13404 KB Output is correct
9 Correct 2 ms 13404 KB Output is correct
10 Correct 2 ms 13404 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 2 ms 13404 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 4 ms 13656 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13532 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 3 ms 12768 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 13 ms 12380 KB Output is correct
23 Correct 13 ms 12376 KB Output is correct
24 Correct 13 ms 12376 KB Output is correct
25 Correct 138 ms 325720 KB Output is correct
26 Correct 39 ms 325700 KB Output is correct
27 Correct 43 ms 325856 KB Output is correct
28 Correct 39 ms 325688 KB Output is correct
29 Correct 41 ms 325664 KB Output is correct
30 Correct 41 ms 325720 KB Output is correct
31 Correct 602 ms 327008 KB Output is correct
32 Correct 610 ms 326876 KB Output is correct
33 Correct 615 ms 326828 KB Output is correct
34 Correct 598 ms 326932 KB Output is correct
35 Correct 603 ms 326996 KB Output is correct
36 Correct 591 ms 326916 KB Output is correct
37 Correct 965 ms 321124 KB Output is correct
38 Correct 959 ms 321360 KB Output is correct
39 Correct 984 ms 321168 KB Output is correct
40 Correct 18 ms 15708 KB Output is correct
41 Correct 11 ms 11356 KB Output is correct
42 Correct 24 ms 16052 KB Output is correct
43 Correct 15 ms 13404 KB Output is correct
44 Correct 14 ms 11608 KB Output is correct
45 Correct 28 ms 16216 KB Output is correct
46 Incorrect 87 ms 10324 KB Output isn't correct
47 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 4 ms 13400 KB Output is correct
8 Correct 2 ms 13404 KB Output is correct
9 Correct 2 ms 13404 KB Output is correct
10 Correct 2 ms 13404 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 2 ms 13404 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 4 ms 13656 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13532 KB Output is correct
19 Correct 3 ms 12636 KB Output is correct
20 Correct 3 ms 12768 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 1 ms 4440 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 2 ms 9560 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 1 ms 7260 KB Output is correct
30 Correct 2 ms 8536 KB Output is correct
31 Correct 2 ms 13148 KB Output is correct
32 Correct 2 ms 10844 KB Output is correct
33 Correct 3 ms 13148 KB Output is correct
34 Correct 2 ms 13148 KB Output is correct
35 Correct 2 ms 9564 KB Output is correct
36 Correct 2 ms 12888 KB Output is correct
37 Correct 2 ms 10844 KB Output is correct
38 Correct 1 ms 8540 KB Output is correct
39 Correct 2 ms 9564 KB Output is correct
40 Correct 2 ms 8540 KB Output is correct
41 Correct 2 ms 9820 KB Output is correct
42 Correct 2 ms 12636 KB Output is correct
43 Correct 2 ms 9564 KB Output is correct
44 Correct 2 ms 13404 KB Output is correct
45 Correct 2 ms 13404 KB Output is correct
46 Correct 3 ms 11356 KB Output is correct
47 Correct 2 ms 11356 KB Output is correct
48 Correct 2 ms 11356 KB Output is correct
49 Correct 2 ms 11356 KB Output is correct
50 Correct 3 ms 13656 KB Output is correct
51 Correct 3 ms 13660 KB Output is correct
52 Correct 3 ms 13660 KB Output is correct
53 Correct 3 ms 13660 KB Output is correct
54 Correct 3 ms 13660 KB Output is correct
55 Correct 3 ms 13660 KB Output is correct
56 Correct 3 ms 12768 KB Output is correct
57 Correct 3 ms 13660 KB Output is correct
58 Correct 3 ms 12636 KB Output is correct
59 Correct 3 ms 13656 KB Output is correct
60 Correct 3 ms 12636 KB Output is correct
61 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 4 ms 13400 KB Output is correct
10 Correct 2 ms 13404 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 2 ms 13404 KB Output is correct
13 Correct 2 ms 13404 KB Output is correct
14 Correct 2 ms 13404 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13656 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13532 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12768 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 13 ms 12380 KB Output is correct
25 Correct 13 ms 12376 KB Output is correct
26 Correct 13 ms 12376 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 2 ms 9560 KB Output is correct
31 Correct 2 ms 10588 KB Output is correct
32 Correct 1 ms 7260 KB Output is correct
33 Correct 2 ms 8536 KB Output is correct
34 Correct 2 ms 13148 KB Output is correct
35 Correct 2 ms 10844 KB Output is correct
36 Correct 3 ms 13148 KB Output is correct
37 Correct 2 ms 13148 KB Output is correct
38 Correct 2 ms 9564 KB Output is correct
39 Correct 2 ms 12888 KB Output is correct
40 Correct 2 ms 10844 KB Output is correct
41 Correct 1 ms 8540 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 2 ms 8540 KB Output is correct
44 Correct 2 ms 9820 KB Output is correct
45 Correct 2 ms 12636 KB Output is correct
46 Correct 2 ms 9564 KB Output is correct
47 Correct 2 ms 13404 KB Output is correct
48 Correct 2 ms 13404 KB Output is correct
49 Correct 3 ms 11356 KB Output is correct
50 Correct 2 ms 11356 KB Output is correct
51 Correct 2 ms 11356 KB Output is correct
52 Correct 2 ms 11356 KB Output is correct
53 Correct 3 ms 13656 KB Output is correct
54 Correct 3 ms 13660 KB Output is correct
55 Correct 3 ms 13660 KB Output is correct
56 Correct 3 ms 13660 KB Output is correct
57 Correct 3 ms 13660 KB Output is correct
58 Correct 3 ms 13660 KB Output is correct
59 Correct 3 ms 12768 KB Output is correct
60 Correct 3 ms 13660 KB Output is correct
61 Correct 3 ms 12636 KB Output is correct
62 Correct 3 ms 13656 KB Output is correct
63 Correct 3 ms 12636 KB Output is correct
64 Correct 3 ms 13660 KB Output is correct
65 Correct 2 ms 9564 KB Output is correct
66 Correct 2 ms 13404 KB Output is correct
67 Correct 3 ms 13404 KB Output is correct
68 Correct 3 ms 13400 KB Output is correct
69 Correct 3 ms 13400 KB Output is correct
70 Correct 5 ms 13348 KB Output is correct
71 Correct 6 ms 13148 KB Output is correct
72 Correct 4 ms 13148 KB Output is correct
73 Correct 4 ms 12892 KB Output is correct
74 Correct 4 ms 10844 KB Output is correct
75 Correct 4 ms 10716 KB Output is correct
76 Correct 11 ms 8284 KB Output is correct
77 Correct 10 ms 11612 KB Output is correct
78 Correct 13 ms 9820 KB Output is correct
79 Correct 3 ms 13656 KB Output is correct
80 Correct 3 ms 13660 KB Output is correct
81 Correct 4 ms 13660 KB Output is correct
82 Correct 3 ms 13660 KB Output is correct
83 Correct 4 ms 13656 KB Output is correct
84 Correct 6 ms 13660 KB Output is correct
85 Correct 6 ms 13660 KB Output is correct
86 Correct 6 ms 13632 KB Output is correct
87 Correct 6 ms 13660 KB Output is correct
88 Correct 6 ms 13660 KB Output is correct
89 Correct 6 ms 13660 KB Output is correct
90 Correct 18 ms 12636 KB Output is correct
91 Correct 19 ms 12596 KB Output is correct
92 Correct 18 ms 12788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 4 ms 13400 KB Output is correct
10 Correct 2 ms 13404 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 2 ms 13404 KB Output is correct
13 Correct 2 ms 13404 KB Output is correct
14 Correct 2 ms 13404 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13656 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13532 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12768 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 13 ms 12380 KB Output is correct
25 Correct 13 ms 12376 KB Output is correct
26 Correct 13 ms 12376 KB Output is correct
27 Correct 138 ms 325720 KB Output is correct
28 Correct 39 ms 325700 KB Output is correct
29 Correct 43 ms 325856 KB Output is correct
30 Correct 39 ms 325688 KB Output is correct
31 Correct 41 ms 325664 KB Output is correct
32 Correct 41 ms 325720 KB Output is correct
33 Correct 602 ms 327008 KB Output is correct
34 Correct 610 ms 326876 KB Output is correct
35 Correct 615 ms 326828 KB Output is correct
36 Correct 598 ms 326932 KB Output is correct
37 Correct 603 ms 326996 KB Output is correct
38 Correct 591 ms 326916 KB Output is correct
39 Correct 965 ms 321124 KB Output is correct
40 Correct 959 ms 321360 KB Output is correct
41 Correct 984 ms 321168 KB Output is correct
42 Correct 1 ms 2396 KB Output is correct
43 Correct 1 ms 2396 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 2 ms 9560 KB Output is correct
46 Correct 2 ms 10588 KB Output is correct
47 Correct 1 ms 7260 KB Output is correct
48 Correct 2 ms 8536 KB Output is correct
49 Correct 2 ms 13148 KB Output is correct
50 Correct 2 ms 10844 KB Output is correct
51 Correct 3 ms 13148 KB Output is correct
52 Correct 2 ms 13148 KB Output is correct
53 Correct 2 ms 9564 KB Output is correct
54 Correct 2 ms 12888 KB Output is correct
55 Correct 2 ms 10844 KB Output is correct
56 Correct 1 ms 8540 KB Output is correct
57 Correct 2 ms 9564 KB Output is correct
58 Correct 2 ms 8540 KB Output is correct
59 Correct 2 ms 9820 KB Output is correct
60 Correct 2 ms 12636 KB Output is correct
61 Correct 2 ms 9564 KB Output is correct
62 Correct 2 ms 13404 KB Output is correct
63 Correct 2 ms 13404 KB Output is correct
64 Correct 3 ms 11356 KB Output is correct
65 Correct 2 ms 11356 KB Output is correct
66 Correct 2 ms 11356 KB Output is correct
67 Correct 2 ms 11356 KB Output is correct
68 Correct 3 ms 13656 KB Output is correct
69 Correct 3 ms 13660 KB Output is correct
70 Correct 3 ms 13660 KB Output is correct
71 Correct 3 ms 13660 KB Output is correct
72 Correct 3 ms 13660 KB Output is correct
73 Correct 3 ms 13660 KB Output is correct
74 Correct 3 ms 12768 KB Output is correct
75 Correct 3 ms 13660 KB Output is correct
76 Correct 3 ms 12636 KB Output is correct
77 Correct 3 ms 13656 KB Output is correct
78 Correct 3 ms 12636 KB Output is correct
79 Correct 3 ms 13660 KB Output is correct
80 Correct 2 ms 9564 KB Output is correct
81 Correct 2 ms 13404 KB Output is correct
82 Correct 3 ms 13404 KB Output is correct
83 Correct 3 ms 13400 KB Output is correct
84 Correct 3 ms 13400 KB Output is correct
85 Correct 5 ms 13348 KB Output is correct
86 Correct 6 ms 13148 KB Output is correct
87 Correct 4 ms 13148 KB Output is correct
88 Correct 4 ms 12892 KB Output is correct
89 Correct 4 ms 10844 KB Output is correct
90 Correct 4 ms 10716 KB Output is correct
91 Correct 11 ms 8284 KB Output is correct
92 Correct 10 ms 11612 KB Output is correct
93 Correct 13 ms 9820 KB Output is correct
94 Correct 3 ms 13656 KB Output is correct
95 Correct 3 ms 13660 KB Output is correct
96 Correct 4 ms 13660 KB Output is correct
97 Correct 3 ms 13660 KB Output is correct
98 Correct 4 ms 13656 KB Output is correct
99 Correct 6 ms 13660 KB Output is correct
100 Correct 6 ms 13660 KB Output is correct
101 Correct 6 ms 13632 KB Output is correct
102 Correct 6 ms 13660 KB Output is correct
103 Correct 6 ms 13660 KB Output is correct
104 Correct 6 ms 13660 KB Output is correct
105 Correct 18 ms 12636 KB Output is correct
106 Correct 19 ms 12596 KB Output is correct
107 Correct 18 ms 12788 KB Output is correct
108 Correct 37 ms 325200 KB Output is correct
109 Correct 97 ms 237752 KB Output is correct
110 Correct 70 ms 197712 KB Output is correct
111 Correct 65 ms 187476 KB Output is correct
112 Correct 91 ms 246864 KB Output is correct
113 Correct 70 ms 175700 KB Output is correct
114 Correct 287 ms 184472 KB Output is correct
115 Correct 60 ms 167688 KB Output is correct
116 Correct 430 ms 296020 KB Output is correct
117 Correct 220 ms 180492 KB Output is correct
118 Correct 84 ms 208784 KB Output is correct
119 Correct 467 ms 234932 KB Output is correct
120 Correct 550 ms 194064 KB Output is correct
121 Correct 295 ms 196432 KB Output is correct
122 Correct 256 ms 185616 KB Output is correct
123 Correct 138 ms 188884 KB Output is correct
124 Correct 184 ms 301128 KB Output is correct
125 Correct 139 ms 303460 KB Output is correct
126 Incorrect 49 ms 253780 KB Output isn't correct
127 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 4440 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 4 ms 13400 KB Output is correct
10 Correct 2 ms 13404 KB Output is correct
11 Correct 2 ms 13404 KB Output is correct
12 Correct 2 ms 13404 KB Output is correct
13 Correct 2 ms 13404 KB Output is correct
14 Correct 2 ms 13404 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 4 ms 13656 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13532 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12768 KB Output is correct
23 Correct 3 ms 12636 KB Output is correct
24 Correct 13 ms 12380 KB Output is correct
25 Correct 13 ms 12376 KB Output is correct
26 Correct 13 ms 12376 KB Output is correct
27 Correct 138 ms 325720 KB Output is correct
28 Correct 39 ms 325700 KB Output is correct
29 Correct 43 ms 325856 KB Output is correct
30 Correct 39 ms 325688 KB Output is correct
31 Correct 41 ms 325664 KB Output is correct
32 Correct 41 ms 325720 KB Output is correct
33 Correct 602 ms 327008 KB Output is correct
34 Correct 610 ms 326876 KB Output is correct
35 Correct 615 ms 326828 KB Output is correct
36 Correct 598 ms 326932 KB Output is correct
37 Correct 603 ms 326996 KB Output is correct
38 Correct 591 ms 326916 KB Output is correct
39 Correct 965 ms 321124 KB Output is correct
40 Correct 959 ms 321360 KB Output is correct
41 Correct 984 ms 321168 KB Output is correct
42 Correct 18 ms 15708 KB Output is correct
43 Correct 11 ms 11356 KB Output is correct
44 Correct 24 ms 16052 KB Output is correct
45 Correct 15 ms 13404 KB Output is correct
46 Correct 14 ms 11608 KB Output is correct
47 Correct 28 ms 16216 KB Output is correct
48 Incorrect 87 ms 10324 KB Output isn't correct
49 Halted 0 ms 0 KB -