Submission #1060884

# Submission time Handle Problem Language Result Execution time Memory
1060884 2024-08-16T03:59:58 Z sleepntsheep Copy and Paste 3 (JOI22_copypaste3) C++17
25 / 100
3000 ms 158640 KB
#pragma GCC optimize("O3,unroll-loops")

#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <typename T>
using ve = vector<T>;

const int N = 1001;
const ll B = 137, M = 1000000007;

int n;
ll dp[N][N], a, b, c;
char s[N];

ll hs[N], bp[N], ibp[N];

ll bpow(ll a, ll b) {
    if (!b) return 1;
    ll r { bpow(a, b / 2) };
    if (b & 1) return r * r % M * a % M;
    return r * r % M;
}

void do_hash() {
    bp[0] = ibp[0] = 1;
    for (int i = 1; i <= n; ++i) bp[i] = bp[i - 1] * B % M, ibp[i] = bpow(bp[i], M - 2);
    for (int i = 1; i <= n; ++i) hs[i] = bp[i] * s[i] % M, hs[i] = (hs[i] + hs[i - 1]) % M;
}

ll get_hash(int l, int r) {
    return ibp[l] * (hs[r] - hs[l - 1] + M) % M;
}

map<ll, ve<int>> oo, op[N];

int main() {
    scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);

    do_hash();
    memset(dp, 63, sizeof dp);

    for (int i = 1; i <= n; ++i) {
        dp[i][i] = a;
        for (int j = i; j <= n; ++j) 
            oo[get_hash(i, j)].push_back(i);
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = i; j <= n; ++j) {
            ll x = get_hash(i, j);
            auto &v = oo[x];
            auto it = lower_bound(v.begin(), v.end(), i);
            while (it != v.end()) {
                op[i][x].push_back(*it);
                it = lower_bound(v.begin(), v.end(), *it + j - i + 1);
            }
        }
    }

    for (int l = 2; l <= n; ++l) {
        for (int i = 1, j = l; j <= n; ++i, ++j) {
            for (int k = i; k <= j; ++k) {
                int len = k - i + 1;
                ll x = get_hash(i, k);
                auto &oc = op[i][x];
                int cnt = upper_bound(oc.begin(), oc.end(), j - len + 1) - lower_bound(oc.begin(), oc.end(), i);

                int lef = l - cnt * len;
                dp[i][j] = min(dp[i][j], dp[i][k] + lef * a + cnt * c + b);
            }
            dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
        }
    }

    printf("%lld", dp[1][n]);
}

Compilation message

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8280 KB Output is correct
5 Correct 1 ms 8280 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Runtime error 413 ms 158640 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8280 KB Output is correct
5 Correct 1 ms 8280 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 2 ms 8280 KB Output is correct
14 Correct 2 ms 8280 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 1 ms 8284 KB Output is correct
18 Correct 1 ms 8284 KB Output is correct
19 Correct 2 ms 8280 KB Output is correct
20 Correct 2 ms 8296 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 2 ms 8536 KB Output is correct
29 Correct 2 ms 8284 KB Output is correct
30 Correct 2 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 1 ms 8284 KB Output is correct
34 Correct 2 ms 8280 KB Output is correct
35 Correct 1 ms 8284 KB Output is correct
36 Correct 1 ms 8284 KB Output is correct
37 Correct 2 ms 8280 KB Output is correct
38 Correct 1 ms 8284 KB Output is correct
39 Correct 2 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8280 KB Output is correct
5 Correct 1 ms 8280 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 2 ms 8280 KB Output is correct
14 Correct 2 ms 8280 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 1 ms 8284 KB Output is correct
18 Correct 1 ms 8284 KB Output is correct
19 Correct 2 ms 8280 KB Output is correct
20 Correct 2 ms 8296 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 2 ms 8536 KB Output is correct
29 Correct 2 ms 8284 KB Output is correct
30 Correct 2 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 1 ms 8284 KB Output is correct
34 Correct 2 ms 8280 KB Output is correct
35 Correct 1 ms 8284 KB Output is correct
36 Correct 1 ms 8284 KB Output is correct
37 Correct 2 ms 8280 KB Output is correct
38 Correct 1 ms 8284 KB Output is correct
39 Correct 2 ms 8284 KB Output is correct
40 Correct 6 ms 9048 KB Output is correct
41 Correct 40 ms 10840 KB Output is correct
42 Correct 73 ms 12888 KB Output is correct
43 Correct 73 ms 12636 KB Output is correct
44 Correct 75 ms 12636 KB Output is correct
45 Correct 74 ms 12636 KB Output is correct
46 Correct 73 ms 12636 KB Output is correct
47 Correct 70 ms 11352 KB Output is correct
48 Correct 73 ms 11612 KB Output is correct
49 Correct 77 ms 12124 KB Output is correct
50 Correct 76 ms 12120 KB Output is correct
51 Correct 82 ms 11736 KB Output is correct
52 Correct 79 ms 11800 KB Output is correct
53 Correct 72 ms 12612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8280 KB Output is correct
5 Correct 1 ms 8280 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 2 ms 8280 KB Output is correct
14 Correct 2 ms 8280 KB Output is correct
15 Correct 2 ms 8284 KB Output is correct
16 Correct 2 ms 8284 KB Output is correct
17 Correct 1 ms 8284 KB Output is correct
18 Correct 1 ms 8284 KB Output is correct
19 Correct 2 ms 8280 KB Output is correct
20 Correct 2 ms 8296 KB Output is correct
21 Correct 2 ms 8284 KB Output is correct
22 Correct 2 ms 8284 KB Output is correct
23 Correct 2 ms 8284 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 2 ms 8284 KB Output is correct
26 Correct 2 ms 8284 KB Output is correct
27 Correct 2 ms 8284 KB Output is correct
28 Correct 2 ms 8536 KB Output is correct
29 Correct 2 ms 8284 KB Output is correct
30 Correct 2 ms 8284 KB Output is correct
31 Correct 2 ms 8284 KB Output is correct
32 Correct 2 ms 8284 KB Output is correct
33 Correct 1 ms 8284 KB Output is correct
34 Correct 2 ms 8280 KB Output is correct
35 Correct 1 ms 8284 KB Output is correct
36 Correct 1 ms 8284 KB Output is correct
37 Correct 2 ms 8280 KB Output is correct
38 Correct 1 ms 8284 KB Output is correct
39 Correct 2 ms 8284 KB Output is correct
40 Correct 6 ms 9048 KB Output is correct
41 Correct 40 ms 10840 KB Output is correct
42 Correct 73 ms 12888 KB Output is correct
43 Correct 73 ms 12636 KB Output is correct
44 Correct 75 ms 12636 KB Output is correct
45 Correct 74 ms 12636 KB Output is correct
46 Correct 73 ms 12636 KB Output is correct
47 Correct 70 ms 11352 KB Output is correct
48 Correct 73 ms 11612 KB Output is correct
49 Correct 77 ms 12124 KB Output is correct
50 Correct 76 ms 12120 KB Output is correct
51 Correct 82 ms 11736 KB Output is correct
52 Correct 79 ms 11800 KB Output is correct
53 Correct 72 ms 12612 KB Output is correct
54 Correct 987 ms 30368 KB Output is correct
55 Execution timed out 3066 ms 78676 KB Time limit exceeded
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8280 KB Output is correct
2 Correct 2 ms 8280 KB Output is correct
3 Correct 1 ms 8284 KB Output is correct
4 Correct 2 ms 8280 KB Output is correct
5 Correct 1 ms 8280 KB Output is correct
6 Correct 1 ms 8284 KB Output is correct
7 Correct 1 ms 8284 KB Output is correct
8 Correct 1 ms 8280 KB Output is correct
9 Correct 2 ms 8284 KB Output is correct
10 Correct 1 ms 8284 KB Output is correct
11 Correct 2 ms 8280 KB Output is correct
12 Correct 2 ms 8280 KB Output is correct
13 Runtime error 413 ms 158640 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -