Submission #565103

# Submission time Handle Problem Language Result Execution time Memory
565103 2022-05-20T09:37:35 Z clarence Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
281 ms 34016 KB
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
int N;
char S[2510];
ll dp[2510][2510];
ll A, B, C;
int groups = 0;
int nextGroups = 0;
int counter[30];
vector<int> same[1500];
vector<int> same2[1500];
vector<int> temp[30];
int pow2[30];
int nextAnchor[2500];
int main(void) {
    for (int i=0;i<26;i++) {
        pow2[(1<<i)%29] = i;
    }
    scanf("%d %s%lld%lld%lld", &N, S, &A, &B, &C);
    for (int i=1;i<=N;i++) {
        for (int j=0;j+i<=N;j++) {
            dp[j][j+i] = A*i;
        }
    }
    for (int i=1;i<=N;i++) { // length
        for (int j=0;j+i<=N;j++) {
            dp[j][j+i] = min(dp[j][j+i], min(dp[j+1][j+i], dp[j][j+i-1])+A);
        }
        if (i<=N/2) {
            if (i == 1) {
                for (int j=0;j+i<=N;j++) {
                    temp[S[j]-'a'].push_back(j);
                }
                for (int k=0;k<26;k++) {
                    if ((int)temp[k].size()>=2) {
                        same[groups++] = temp[k];
                    }
                    temp[k].clear();
                }
            } else {
                // hash stuff??
                nextGroups = 0;
                int bm = 0;
                for (int k=0;k<groups;k++) {
                    for (int j=0;j<(int)same[k].size();j++) {
                        if (same[k][j]+i > N) continue;
                        bm = bm | (1 << (int)(S[same[k][j]+i-1]-'a'));
                        temp[S[same[k][j]+i-1]-'a'].push_back(same[k][j]);
                    }
                    // bitmask yay
                    while (bm) {
                        int lsb = bm & (-bm);
                        if (temp[pow2[lsb%29]].size() >= 2) {
                            same2[nextGroups++] = temp[pow2[lsb%29]];
                        }
                        temp[pow2[lsb%29]].clear();
                        bm -= lsb;
                    }
                }
                for (int k=0;k<nextGroups;k++) {
                    same[k] = same2[k];
                }
                groups = nextGroups;
            }
            for (int k=0;k<groups;k++) {
                int ptr = 0;
                int L = (int)same[k].size();
                for (int j=0;j<L;j++) {
                    // try making from here
                    // same[k] should be sorted
                    while (ptr < L && same[k][j]+i>same[k][ptr]) ptr++;
                    nextAnchor[j] = ptr;
                }
                ll dpValue = dp[same[k][0]][same[k][0]+i];
                for (int j=0;j<L;j++) {
                    // try making from here
                    // same[k] should be sorted
                    int cur = nextAnchor[j];
                    int cnt = 2;
                    int start = same[k][j];
                    while (cur < L) {
                        int stop = same[k][cur]+i;
                        dp[start][stop] = min(
                            dp[start][stop],
                            cnt * C + B + dpValue + A * (stop - start - cnt * i)
                        );
                        cur = nextAnchor[cur];
                        cnt++;
                    }
                }
            }
        }
    }
    /*for (int i=0;i<=N;i++) {
        for (int j=i;j<=N;j++) {
            printf("%lld ", dp[i][j]);
        } printf("\n");
    }*/
    printf("%lld\n", dp[0][N]);
    return 0;
}

Compilation message

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     scanf("%d %s%lld%lld%lld", &N, S, &A, &B, &C);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 0 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 145 ms 20836 KB Output is correct
4 Correct 162 ms 23504 KB Output is correct
5 Correct 185 ms 26828 KB Output is correct
6 Correct 251 ms 30188 KB Output is correct
7 Correct 281 ms 33904 KB Output is correct
8 Correct 268 ms 33916 KB Output is correct
9 Correct 271 ms 33916 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 376 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 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 0 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 1 ms 340 KB Output is correct
15 Correct 1 ms 376 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 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 468 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 1 ms 468 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 1 ms 340 KB Output is correct
35 Correct 1 ms 384 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 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 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 0 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 1 ms 340 KB Output is correct
15 Correct 1 ms 376 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 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 468 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 1 ms 468 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 1 ms 340 KB Output is correct
35 Correct 1 ms 384 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 340 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 1236 KB Output is correct
42 Correct 1 ms 1236 KB Output is correct
43 Correct 1 ms 1236 KB Output is correct
44 Correct 1 ms 1236 KB Output is correct
45 Correct 1 ms 1272 KB Output is correct
46 Correct 1 ms 1272 KB Output is correct
47 Correct 1 ms 1236 KB Output is correct
48 Correct 1 ms 1236 KB Output is correct
49 Correct 1 ms 1236 KB Output is correct
50 Correct 2 ms 1272 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1272 KB Output is correct
53 Correct 1 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 0 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 1 ms 340 KB Output is correct
15 Correct 1 ms 376 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 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 468 KB Output is correct
24 Correct 0 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 0 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 0 ms 468 KB Output is correct
29 Correct 1 ms 468 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 1 ms 340 KB Output is correct
35 Correct 1 ms 384 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 340 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 1236 KB Output is correct
42 Correct 1 ms 1236 KB Output is correct
43 Correct 1 ms 1236 KB Output is correct
44 Correct 1 ms 1236 KB Output is correct
45 Correct 1 ms 1272 KB Output is correct
46 Correct 1 ms 1272 KB Output is correct
47 Correct 1 ms 1236 KB Output is correct
48 Correct 1 ms 1236 KB Output is correct
49 Correct 1 ms 1236 KB Output is correct
50 Correct 2 ms 1272 KB Output is correct
51 Correct 2 ms 1236 KB Output is correct
52 Correct 2 ms 1272 KB Output is correct
53 Correct 1 ms 1236 KB Output is correct
54 Correct 3 ms 2900 KB Output is correct
55 Correct 26 ms 8328 KB Output is correct
56 Correct 8 ms 8276 KB Output is correct
57 Correct 7 ms 8308 KB Output is correct
58 Correct 6 ms 8276 KB Output is correct
59 Correct 5 ms 8276 KB Output is correct
60 Correct 6 ms 8304 KB Output is correct
61 Correct 18 ms 8340 KB Output is correct
62 Correct 24 ms 8320 KB Output is correct
63 Correct 12 ms 8216 KB Output is correct
64 Correct 13 ms 8256 KB Output is correct
65 Correct 21 ms 8276 KB Output is correct
66 Correct 21 ms 8276 KB Output is correct
67 Correct 5 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 372 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 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 0 ms 340 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 145 ms 20836 KB Output is correct
14 Correct 162 ms 23504 KB Output is correct
15 Correct 185 ms 26828 KB Output is correct
16 Correct 251 ms 30188 KB Output is correct
17 Correct 281 ms 33904 KB Output is correct
18 Correct 268 ms 33916 KB Output is correct
19 Correct 271 ms 33916 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 376 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 376 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 376 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 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 468 KB Output is correct
41 Correct 0 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 0 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 0 ms 468 KB Output is correct
46 Correct 1 ms 468 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 1 ms 340 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 1 ms 340 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 724 KB Output is correct
58 Correct 1 ms 1236 KB Output is correct
59 Correct 1 ms 1236 KB Output is correct
60 Correct 1 ms 1236 KB Output is correct
61 Correct 1 ms 1236 KB Output is correct
62 Correct 1 ms 1272 KB Output is correct
63 Correct 1 ms 1272 KB Output is correct
64 Correct 1 ms 1236 KB Output is correct
65 Correct 1 ms 1236 KB Output is correct
66 Correct 1 ms 1236 KB Output is correct
67 Correct 2 ms 1272 KB Output is correct
68 Correct 2 ms 1236 KB Output is correct
69 Correct 2 ms 1272 KB Output is correct
70 Correct 1 ms 1236 KB Output is correct
71 Correct 3 ms 2900 KB Output is correct
72 Correct 26 ms 8328 KB Output is correct
73 Correct 8 ms 8276 KB Output is correct
74 Correct 7 ms 8308 KB Output is correct
75 Correct 6 ms 8276 KB Output is correct
76 Correct 5 ms 8276 KB Output is correct
77 Correct 6 ms 8304 KB Output is correct
78 Correct 18 ms 8340 KB Output is correct
79 Correct 24 ms 8320 KB Output is correct
80 Correct 12 ms 8216 KB Output is correct
81 Correct 13 ms 8256 KB Output is correct
82 Correct 21 ms 8276 KB Output is correct
83 Correct 21 ms 8276 KB Output is correct
84 Correct 5 ms 8312 KB Output is correct
85 Correct 21 ms 16460 KB Output is correct
86 Correct 77 ms 34016 KB Output is correct
87 Correct 70 ms 33916 KB Output is correct
88 Correct 65 ms 33980 KB Output is correct
89 Correct 71 ms 33956 KB Output is correct
90 Correct 61 ms 33868 KB Output is correct
91 Correct 170 ms 33916 KB Output is correct
92 Correct 214 ms 33840 KB Output is correct
93 Correct 129 ms 33956 KB Output is correct
94 Correct 101 ms 33876 KB Output is correct
95 Correct 202 ms 34012 KB Output is correct
96 Correct 224 ms 33996 KB Output is correct
97 Correct 59 ms 33912 KB Output is correct