Submission #637164

# Submission time Handle Problem Language Result Execution time Memory
637164 2022-08-31T16:42:36 Z tht2005 Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
543 ms 54008 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T> void ckmin(T& a, T b) {
    if(a > b) {
        a = b;
    }
}

#define LL long long

#define N 2503

void calc_z(char* s, int n, int* z) {
    z[0] = n;
    for(int i = 1, l = 0, r = 0; i < n; ++i) {
        int k = (i > r) ? 0 : min(z[i - l], r - i + 1);
        while(i + k < n && s[k] == s[i + k]) {
            ++k;
        }
        z[i] = k--;
        if(r < i + k) {
            l = i;
            r = i + k;
        }
    }
}

int n, A, B, C, z[N], nxt[N][N];
char s[N];
LL f[N][N];

int main() {
    scanf("%d %s %d %d %d", &n, s, &A, &B, &C);
    for(int i = 0, k; i < n; ++i) {
        calc_z(s + i, n - i, z + i);
        k = i;
        for(int j = i + 1; j < n; ++j) {
            while(k < j && z[j] >= k + 1 - i) {
                nxt[i][++k] = j;
            }
        }
        while(k < n) {
            nxt[i][++k] = -1;
        }
    }
    for(int i = 0; i < n; ++i) {
        for(int j = i; j <= n; ++j) {
            f[i][j] = (long long)A * (j - i);
        }
    }
    for(int L = 1; L < n; ++L) {
        for(int i = 0, j = L; j <= n; ++i, ++j) {
            if(i) {
                ckmin(f[i - 1][j], f[i][j] + A);
            }
            if(j != n) {
                ckmin(f[i][j + 1], f[i][j] + A);
            }
            long long cost = f[i][j] + B + C;
            int x = i;
            while(1) {
                int y = nxt[x][x + L];
                if(y == -1) {
                    break;
                }
                cost += (long long)A * (y - x - L) + C;
                x = y;
                ckmin(f[i][x + L], cost);
            }
        }
    }
    printf("%lld", f[0][n]);
    return 0;
}

Compilation message

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |     scanf("%d %s %d %d %d", &n, s, &A, &B, &C);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 444 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 256 ms 34332 KB Output is correct
4 Correct 279 ms 38548 KB Output is correct
5 Correct 387 ms 43296 KB Output is correct
6 Correct 431 ms 48464 KB Output is correct
7 Correct 543 ms 53880 KB Output is correct
8 Correct 535 ms 53908 KB Output is correct
9 Correct 518 ms 53900 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 312 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 568 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 568 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 564 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 308 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 312 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 568 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 568 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 564 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 308 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 980 KB Output is correct
41 Correct 2 ms 2132 KB Output is correct
42 Correct 2 ms 2132 KB Output is correct
43 Correct 2 ms 2132 KB Output is correct
44 Correct 2 ms 2104 KB Output is correct
45 Correct 2 ms 2132 KB Output is correct
46 Correct 1 ms 2132 KB Output is correct
47 Correct 2 ms 2104 KB Output is correct
48 Correct 1 ms 2032 KB Output is correct
49 Correct 1 ms 2100 KB Output is correct
50 Correct 2 ms 2132 KB Output is correct
51 Correct 2 ms 2096 KB Output is correct
52 Correct 2 ms 2132 KB Output is correct
53 Correct 1 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 468 KB Output is correct
14 Correct 0 ms 312 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 568 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 568 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 564 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 1 ms 308 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 980 KB Output is correct
41 Correct 2 ms 2132 KB Output is correct
42 Correct 2 ms 2132 KB Output is correct
43 Correct 2 ms 2132 KB Output is correct
44 Correct 2 ms 2104 KB Output is correct
45 Correct 2 ms 2132 KB Output is correct
46 Correct 1 ms 2132 KB Output is correct
47 Correct 2 ms 2104 KB Output is correct
48 Correct 1 ms 2032 KB Output is correct
49 Correct 1 ms 2100 KB Output is correct
50 Correct 2 ms 2132 KB Output is correct
51 Correct 2 ms 2096 KB Output is correct
52 Correct 2 ms 2132 KB Output is correct
53 Correct 1 ms 2132 KB Output is correct
54 Correct 5 ms 4948 KB Output is correct
55 Correct 49 ms 14256 KB Output is correct
56 Correct 23 ms 14168 KB Output is correct
57 Correct 18 ms 14140 KB Output is correct
58 Correct 16 ms 14216 KB Output is correct
59 Correct 14 ms 14168 KB Output is correct
60 Correct 13 ms 14132 KB Output is correct
61 Correct 19 ms 14140 KB Output is correct
62 Correct 39 ms 14176 KB Output is correct
63 Correct 16 ms 14168 KB Output is correct
64 Correct 17 ms 14116 KB Output is correct
65 Correct 27 ms 14200 KB Output is correct
66 Correct 27 ms 14164 KB Output is correct
67 Correct 12 ms 14164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 312 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 444 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 256 ms 34332 KB Output is correct
14 Correct 279 ms 38548 KB Output is correct
15 Correct 387 ms 43296 KB Output is correct
16 Correct 431 ms 48464 KB Output is correct
17 Correct 543 ms 53880 KB Output is correct
18 Correct 535 ms 53908 KB Output is correct
19 Correct 518 ms 53900 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 308 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 468 KB Output is correct
31 Correct 0 ms 312 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 312 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 0 ms 568 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 568 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 564 KB Output is correct
47 Correct 1 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 468 KB Output is correct
50 Correct 1 ms 468 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 ms 212 KB Output is correct
53 Correct 1 ms 308 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 1 ms 980 KB Output is correct
58 Correct 2 ms 2132 KB Output is correct
59 Correct 2 ms 2132 KB Output is correct
60 Correct 2 ms 2132 KB Output is correct
61 Correct 2 ms 2104 KB Output is correct
62 Correct 2 ms 2132 KB Output is correct
63 Correct 1 ms 2132 KB Output is correct
64 Correct 2 ms 2104 KB Output is correct
65 Correct 1 ms 2032 KB Output is correct
66 Correct 1 ms 2100 KB Output is correct
67 Correct 2 ms 2132 KB Output is correct
68 Correct 2 ms 2096 KB Output is correct
69 Correct 2 ms 2132 KB Output is correct
70 Correct 1 ms 2132 KB Output is correct
71 Correct 5 ms 4948 KB Output is correct
72 Correct 49 ms 14256 KB Output is correct
73 Correct 23 ms 14168 KB Output is correct
74 Correct 18 ms 14140 KB Output is correct
75 Correct 16 ms 14216 KB Output is correct
76 Correct 14 ms 14168 KB Output is correct
77 Correct 13 ms 14132 KB Output is correct
78 Correct 19 ms 14140 KB Output is correct
79 Correct 39 ms 14176 KB Output is correct
80 Correct 16 ms 14168 KB Output is correct
81 Correct 17 ms 14116 KB Output is correct
82 Correct 27 ms 14200 KB Output is correct
83 Correct 27 ms 14164 KB Output is correct
84 Correct 12 ms 14164 KB Output is correct
85 Correct 57 ms 27612 KB Output is correct
86 Correct 151 ms 53896 KB Output is correct
87 Correct 131 ms 53832 KB Output is correct
88 Correct 96 ms 53896 KB Output is correct
89 Correct 89 ms 53904 KB Output is correct
90 Correct 85 ms 53896 KB Output is correct
91 Correct 271 ms 53900 KB Output is correct
92 Correct 384 ms 53900 KB Output is correct
93 Correct 121 ms 53836 KB Output is correct
94 Correct 140 ms 53896 KB Output is correct
95 Correct 332 ms 54008 KB Output is correct
96 Correct 266 ms 53804 KB Output is correct
97 Correct 84 ms 53820 KB Output is correct