Submission #776772

# Submission time Handle Problem Language Result Execution time Memory
776772 2023-07-08T08:55:06 Z danikoynov Copy and Paste 3 (JOI22_copypaste3) C++14
62 / 100
3000 ms 898840 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


const int maxn = 2510;
const ll base = 29, inf = 1e18;

int n;
ll a, b, c, h[maxn], pw[maxn];
string s;
vector < int >  occ[maxn * maxn];
ll hs_dp[maxn * maxn], pt[maxn * maxn];
unordered_map < int, int > nxt[maxn * maxn];
ll range_hash[maxn][maxn];
ll get_hash(int i, int j)
{
    return range_hash[i][j];
}

unordered_map < ll, ll > rev;
void solve()
{
    cin >> n >> s >> a >> b >> c;
    s = "#" + s;
    pw[0] = 1;
    for (int i = 1; i <= n; i ++)
    {
        h[i] = h[i - 1] * base + (s[i] - 'a' + 1);
        pw[i] = pw[i - 1] * base;
    }
    vector < ll > cp;
    for (int i = 1; i <= n; i ++)
        for (int j = i; j <= n; j ++)
    {
        range_hash[i][j] = h[j] - h[i - 1] * pw[j - i + 1];
        cp.push_back(range_hash[i][j]);
    }
    sort(cp.begin(), cp.end());
    int cnt = 0;
    for (int i = 0; i < cp.size(); i ++)
    {
        if (i == 0 || cp[i] != cp[i - 1])
            rev[cp[i]] = ++ cnt;
    }

    for (int i = 1; i <= n; i ++)
        for (int j = i; j <= n; j ++)
        range_hash[i][j] = rev[range_hash[i][j]];


    for (int i = n; i > 0; i --)
    {


        for (int p = i; p <= n; p ++)
        {
            ll cur_hs = get_hash(i, p);
            occ[cur_hs].push_back(i);
            /**cout << "hash " << i << " " << p << endl;
            cout << "occurrences: ";
            for (int ds : occ[cur_hs])
                cout << ds << " ";
            cout << endl;*/
            if (occ[cur_hs].size() == 1)
            {
                hs_dp[cur_hs] = hs_dp[range_hash[i + 1][p]] + a;
                nxt[cur_hs][p] = n + 1;
                pt[cur_hs] = -1;
            }
            else
            {
                hs_dp[cur_hs] = min(hs_dp[cur_hs], hs_dp[range_hash[i + 1][p]] + a);
                if (pt[cur_hs] == -1)
                {
                    pt[cur_hs] ++;
                }
                while(occ[cur_hs][pt[cur_hs]] > p)
                    pt[cur_hs] ++;
                pt[cur_hs] --;
                if (pt[cur_hs] == -1)
                    nxt[cur_hs][p] = n + 1;
                else
                    nxt[cur_hs][p] = occ[cur_hs][pt[cur_hs]] + (p - i);
                    ///cout << "step " << i << " " << p << " " << nxt[p][cur_hs] << endl;
            }
        }


        for (int k = i; k <= n; k ++)
        {
            ll clip_hash, clip_len = k - i + 1;
            clip_hash = get_hash(i, k);
            ll cost = hs_dp[clip_hash] + b + c;
            if (occ[get_hash(i, k)].size() < 2)
                break;
            int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
            ///dp[get_hash(i, k)] = min(dp[get_hash(i, k)], cos);
            ///cout << "-------------------" << endl;
            ///cout << i << " " << k << endl;
            while(pos <= n)
            {
                ///cout << pos << " " << cost << endl;
                int to = nxt[clip_hash][pos];
                if (to > n)
                    break;
                cost = cost + (to - pos - clip_len) * a;
                cost = cost + c;
                ///cout << "stop " << occ[clip_hash][pt] << endl;
                ///cost = cost + (occ[clip_hash][pt] - pos) * a;
                ///cost = cost + c;
                pos = to;
                hs_dp[range_hash[i][pos]] = min(hs_dp[range_hash[i][pos]], cost);

                /**int to = pos + clip_len - 1;

                if (to > n)
                {
                    break;
                }

                if (get_hash(pos, to) == clip_hash)
                {

                    cost = cost + c;
                    pos = pos + clip_len;
                    hs_dp[get_hash(i, to)] = min(hs_dp[get_hash(i, to)], cost);
                }
                else
                {
                    pos ++;
                    cost += a;
                }*/
            }

        }
        for (int k = i; k <= n; k ++)
        {
            hs_dp[range_hash[i][k]] = min(hs_dp[range_hash[i][k]], hs_dp[range_hash[i][k - 1]] + a);
        }

    }

    /**for (int i = 1; i <= n; i ++, cout << endl)
        for (int j = i; j <= n; j ++)
            cout << hs_dp[get_hash(i, j)] << " ";*/

    cout << hs_dp[range_hash[1][n]] << endl;


}

int main()
{
    solve();
    return 0;
}

Compilation message

copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for (int i = 0; i < cp.size(); i ++)
      |                     ~~^~~~~~~~~~~
copypaste3.cpp:114:26: warning: unused variable 'pt' [-Wunused-variable]
  114 |             int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
      |                          ^~
# Verdict Execution time Memory Grader output
1 Correct 238 ms 493388 KB Output is correct
2 Correct 205 ms 493484 KB Output is correct
3 Correct 207 ms 493364 KB Output is correct
4 Correct 205 ms 493436 KB Output is correct
5 Correct 224 ms 493620 KB Output is correct
6 Correct 209 ms 493420 KB Output is correct
7 Correct 210 ms 493484 KB Output is correct
8 Correct 211 ms 493488 KB Output is correct
9 Correct 228 ms 493448 KB Output is correct
10 Correct 236 ms 493388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 213 ms 493564 KB Output is correct
2 Correct 209 ms 493412 KB Output is correct
3 Correct 1293 ms 608492 KB Output is correct
4 Correct 1525 ms 626436 KB Output is correct
5 Correct 1816 ms 647148 KB Output is correct
6 Correct 2055 ms 671060 KB Output is correct
7 Correct 2625 ms 701760 KB Output is correct
8 Correct 2532 ms 701680 KB Output is correct
9 Correct 2550 ms 701784 KB Output is correct
10 Correct 216 ms 493480 KB Output is correct
11 Correct 211 ms 493448 KB Output is correct
12 Correct 214 ms 493380 KB Output is correct
13 Correct 212 ms 493460 KB Output is correct
14 Correct 235 ms 493540 KB Output is correct
15 Correct 209 ms 493520 KB Output is correct
16 Correct 210 ms 493632 KB Output is correct
17 Correct 213 ms 493540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 493388 KB Output is correct
2 Correct 205 ms 493484 KB Output is correct
3 Correct 207 ms 493364 KB Output is correct
4 Correct 205 ms 493436 KB Output is correct
5 Correct 224 ms 493620 KB Output is correct
6 Correct 209 ms 493420 KB Output is correct
7 Correct 210 ms 493484 KB Output is correct
8 Correct 211 ms 493488 KB Output is correct
9 Correct 228 ms 493448 KB Output is correct
10 Correct 236 ms 493388 KB Output is correct
11 Correct 218 ms 493480 KB Output is correct
12 Correct 231 ms 493556 KB Output is correct
13 Correct 210 ms 493472 KB Output is correct
14 Correct 213 ms 493520 KB Output is correct
15 Correct 211 ms 493576 KB Output is correct
16 Correct 225 ms 493508 KB Output is correct
17 Correct 210 ms 493460 KB Output is correct
18 Correct 222 ms 493488 KB Output is correct
19 Correct 216 ms 493648 KB Output is correct
20 Correct 214 ms 493524 KB Output is correct
21 Correct 220 ms 493620 KB Output is correct
22 Correct 219 ms 493764 KB Output is correct
23 Correct 230 ms 493644 KB Output is correct
24 Correct 269 ms 493708 KB Output is correct
25 Correct 225 ms 493696 KB Output is correct
26 Correct 219 ms 493720 KB Output is correct
27 Correct 213 ms 493644 KB Output is correct
28 Correct 213 ms 493560 KB Output is correct
29 Correct 221 ms 493700 KB Output is correct
30 Correct 229 ms 493692 KB Output is correct
31 Correct 222 ms 493608 KB Output is correct
32 Correct 248 ms 493684 KB Output is correct
33 Correct 221 ms 493688 KB Output is correct
34 Correct 213 ms 493476 KB Output is correct
35 Correct 209 ms 493388 KB Output is correct
36 Correct 212 ms 493468 KB Output is correct
37 Correct 215 ms 493488 KB Output is correct
38 Correct 212 ms 493520 KB Output is correct
39 Correct 230 ms 493612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 493388 KB Output is correct
2 Correct 205 ms 493484 KB Output is correct
3 Correct 207 ms 493364 KB Output is correct
4 Correct 205 ms 493436 KB Output is correct
5 Correct 224 ms 493620 KB Output is correct
6 Correct 209 ms 493420 KB Output is correct
7 Correct 210 ms 493484 KB Output is correct
8 Correct 211 ms 493488 KB Output is correct
9 Correct 228 ms 493448 KB Output is correct
10 Correct 236 ms 493388 KB Output is correct
11 Correct 218 ms 493480 KB Output is correct
12 Correct 231 ms 493556 KB Output is correct
13 Correct 210 ms 493472 KB Output is correct
14 Correct 213 ms 493520 KB Output is correct
15 Correct 211 ms 493576 KB Output is correct
16 Correct 225 ms 493508 KB Output is correct
17 Correct 210 ms 493460 KB Output is correct
18 Correct 222 ms 493488 KB Output is correct
19 Correct 216 ms 493648 KB Output is correct
20 Correct 214 ms 493524 KB Output is correct
21 Correct 220 ms 493620 KB Output is correct
22 Correct 219 ms 493764 KB Output is correct
23 Correct 230 ms 493644 KB Output is correct
24 Correct 269 ms 493708 KB Output is correct
25 Correct 225 ms 493696 KB Output is correct
26 Correct 219 ms 493720 KB Output is correct
27 Correct 213 ms 493644 KB Output is correct
28 Correct 213 ms 493560 KB Output is correct
29 Correct 221 ms 493700 KB Output is correct
30 Correct 229 ms 493692 KB Output is correct
31 Correct 222 ms 493608 KB Output is correct
32 Correct 248 ms 493684 KB Output is correct
33 Correct 221 ms 493688 KB Output is correct
34 Correct 213 ms 493476 KB Output is correct
35 Correct 209 ms 493388 KB Output is correct
36 Correct 212 ms 493468 KB Output is correct
37 Correct 215 ms 493488 KB Output is correct
38 Correct 212 ms 493520 KB Output is correct
39 Correct 230 ms 493612 KB Output is correct
40 Correct 216 ms 494640 KB Output is correct
41 Correct 214 ms 495508 KB Output is correct
42 Correct 231 ms 498852 KB Output is correct
43 Correct 268 ms 498948 KB Output is correct
44 Correct 233 ms 499012 KB Output is correct
45 Correct 241 ms 499108 KB Output is correct
46 Correct 254 ms 499080 KB Output is correct
47 Correct 221 ms 496148 KB Output is correct
48 Correct 253 ms 497596 KB Output is correct
49 Correct 238 ms 498244 KB Output is correct
50 Correct 249 ms 498204 KB Output is correct
51 Correct 222 ms 497352 KB Output is correct
52 Correct 236 ms 497488 KB Output is correct
53 Correct 235 ms 499068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 493388 KB Output is correct
2 Correct 205 ms 493484 KB Output is correct
3 Correct 207 ms 493364 KB Output is correct
4 Correct 205 ms 493436 KB Output is correct
5 Correct 224 ms 493620 KB Output is correct
6 Correct 209 ms 493420 KB Output is correct
7 Correct 210 ms 493484 KB Output is correct
8 Correct 211 ms 493488 KB Output is correct
9 Correct 228 ms 493448 KB Output is correct
10 Correct 236 ms 493388 KB Output is correct
11 Correct 218 ms 493480 KB Output is correct
12 Correct 231 ms 493556 KB Output is correct
13 Correct 210 ms 493472 KB Output is correct
14 Correct 213 ms 493520 KB Output is correct
15 Correct 211 ms 493576 KB Output is correct
16 Correct 225 ms 493508 KB Output is correct
17 Correct 210 ms 493460 KB Output is correct
18 Correct 222 ms 493488 KB Output is correct
19 Correct 216 ms 493648 KB Output is correct
20 Correct 214 ms 493524 KB Output is correct
21 Correct 220 ms 493620 KB Output is correct
22 Correct 219 ms 493764 KB Output is correct
23 Correct 230 ms 493644 KB Output is correct
24 Correct 269 ms 493708 KB Output is correct
25 Correct 225 ms 493696 KB Output is correct
26 Correct 219 ms 493720 KB Output is correct
27 Correct 213 ms 493644 KB Output is correct
28 Correct 213 ms 493560 KB Output is correct
29 Correct 221 ms 493700 KB Output is correct
30 Correct 229 ms 493692 KB Output is correct
31 Correct 222 ms 493608 KB Output is correct
32 Correct 248 ms 493684 KB Output is correct
33 Correct 221 ms 493688 KB Output is correct
34 Correct 213 ms 493476 KB Output is correct
35 Correct 209 ms 493388 KB Output is correct
36 Correct 212 ms 493468 KB Output is correct
37 Correct 215 ms 493488 KB Output is correct
38 Correct 212 ms 493520 KB Output is correct
39 Correct 230 ms 493612 KB Output is correct
40 Correct 216 ms 494640 KB Output is correct
41 Correct 214 ms 495508 KB Output is correct
42 Correct 231 ms 498852 KB Output is correct
43 Correct 268 ms 498948 KB Output is correct
44 Correct 233 ms 499012 KB Output is correct
45 Correct 241 ms 499108 KB Output is correct
46 Correct 254 ms 499080 KB Output is correct
47 Correct 221 ms 496148 KB Output is correct
48 Correct 253 ms 497596 KB Output is correct
49 Correct 238 ms 498244 KB Output is correct
50 Correct 249 ms 498204 KB Output is correct
51 Correct 222 ms 497352 KB Output is correct
52 Correct 236 ms 497488 KB Output is correct
53 Correct 235 ms 499068 KB Output is correct
54 Correct 330 ms 519460 KB Output is correct
55 Correct 489 ms 529200 KB Output is correct
56 Correct 833 ms 618996 KB Output is correct
57 Correct 833 ms 619596 KB Output is correct
58 Correct 821 ms 620088 KB Output is correct
59 Correct 778 ms 620208 KB Output is correct
60 Correct 795 ms 620288 KB Output is correct
61 Correct 567 ms 563692 KB Output is correct
62 Correct 435 ms 530224 KB Output is correct
63 Correct 745 ms 596800 KB Output is correct
64 Correct 709 ms 597632 KB Output is correct
65 Correct 639 ms 570452 KB Output is correct
66 Correct 574 ms 570416 KB Output is correct
67 Correct 823 ms 620416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 238 ms 493388 KB Output is correct
2 Correct 205 ms 493484 KB Output is correct
3 Correct 207 ms 493364 KB Output is correct
4 Correct 205 ms 493436 KB Output is correct
5 Correct 224 ms 493620 KB Output is correct
6 Correct 209 ms 493420 KB Output is correct
7 Correct 210 ms 493484 KB Output is correct
8 Correct 211 ms 493488 KB Output is correct
9 Correct 228 ms 493448 KB Output is correct
10 Correct 236 ms 493388 KB Output is correct
11 Correct 213 ms 493564 KB Output is correct
12 Correct 209 ms 493412 KB Output is correct
13 Correct 1293 ms 608492 KB Output is correct
14 Correct 1525 ms 626436 KB Output is correct
15 Correct 1816 ms 647148 KB Output is correct
16 Correct 2055 ms 671060 KB Output is correct
17 Correct 2625 ms 701760 KB Output is correct
18 Correct 2532 ms 701680 KB Output is correct
19 Correct 2550 ms 701784 KB Output is correct
20 Correct 216 ms 493480 KB Output is correct
21 Correct 211 ms 493448 KB Output is correct
22 Correct 214 ms 493380 KB Output is correct
23 Correct 212 ms 493460 KB Output is correct
24 Correct 235 ms 493540 KB Output is correct
25 Correct 209 ms 493520 KB Output is correct
26 Correct 210 ms 493632 KB Output is correct
27 Correct 213 ms 493540 KB Output is correct
28 Correct 218 ms 493480 KB Output is correct
29 Correct 231 ms 493556 KB Output is correct
30 Correct 210 ms 493472 KB Output is correct
31 Correct 213 ms 493520 KB Output is correct
32 Correct 211 ms 493576 KB Output is correct
33 Correct 225 ms 493508 KB Output is correct
34 Correct 210 ms 493460 KB Output is correct
35 Correct 222 ms 493488 KB Output is correct
36 Correct 216 ms 493648 KB Output is correct
37 Correct 214 ms 493524 KB Output is correct
38 Correct 220 ms 493620 KB Output is correct
39 Correct 219 ms 493764 KB Output is correct
40 Correct 230 ms 493644 KB Output is correct
41 Correct 269 ms 493708 KB Output is correct
42 Correct 225 ms 493696 KB Output is correct
43 Correct 219 ms 493720 KB Output is correct
44 Correct 213 ms 493644 KB Output is correct
45 Correct 213 ms 493560 KB Output is correct
46 Correct 221 ms 493700 KB Output is correct
47 Correct 229 ms 493692 KB Output is correct
48 Correct 222 ms 493608 KB Output is correct
49 Correct 248 ms 493684 KB Output is correct
50 Correct 221 ms 493688 KB Output is correct
51 Correct 213 ms 493476 KB Output is correct
52 Correct 209 ms 493388 KB Output is correct
53 Correct 212 ms 493468 KB Output is correct
54 Correct 215 ms 493488 KB Output is correct
55 Correct 212 ms 493520 KB Output is correct
56 Correct 230 ms 493612 KB Output is correct
57 Correct 216 ms 494640 KB Output is correct
58 Correct 214 ms 495508 KB Output is correct
59 Correct 231 ms 498852 KB Output is correct
60 Correct 268 ms 498948 KB Output is correct
61 Correct 233 ms 499012 KB Output is correct
62 Correct 241 ms 499108 KB Output is correct
63 Correct 254 ms 499080 KB Output is correct
64 Correct 221 ms 496148 KB Output is correct
65 Correct 253 ms 497596 KB Output is correct
66 Correct 238 ms 498244 KB Output is correct
67 Correct 249 ms 498204 KB Output is correct
68 Correct 222 ms 497352 KB Output is correct
69 Correct 236 ms 497488 KB Output is correct
70 Correct 235 ms 499068 KB Output is correct
71 Correct 330 ms 519460 KB Output is correct
72 Correct 489 ms 529200 KB Output is correct
73 Correct 833 ms 618996 KB Output is correct
74 Correct 833 ms 619596 KB Output is correct
75 Correct 821 ms 620088 KB Output is correct
76 Correct 778 ms 620208 KB Output is correct
77 Correct 795 ms 620288 KB Output is correct
78 Correct 567 ms 563692 KB Output is correct
79 Correct 435 ms 530224 KB Output is correct
80 Correct 745 ms 596800 KB Output is correct
81 Correct 709 ms 597632 KB Output is correct
82 Correct 639 ms 570452 KB Output is correct
83 Correct 574 ms 570416 KB Output is correct
84 Correct 823 ms 620416 KB Output is correct
85 Correct 2083 ms 802276 KB Output is correct
86 Execution timed out 3114 ms 898840 KB Time limit exceeded
87 Halted 0 ms 0 KB -