답안 #574126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
574126 2022-06-07T23:58:52 Z chonka Copy and Paste 3 (JOI22_copypaste3) C++
100 / 100
2385 ms 505316 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 2507 ;
const int BASE = 27 ;

ll MOD[ 2 ] = { 1000000007 , 998244353 } ;

int n ;
string a ;
ll costA , costB , costC ;

struct hsh {
    int l , r ;
    ll val[ 2 ] ;
    void advance ( int nw ) {
        for ( auto i : { 0 , 1 } ) {
            val[ i ] = ( val[ i ] * BASE + nw ) % MOD[ i ] ;
        }
    }
};

hsh aux[ MAXN ][ MAXN ] ;
vector < hsh > srt ;
int eq_class[ MAXN ][ MAXN ] ;
vector < pair < int , int > > class_list[ MAXN * MAXN ] ;

pair < int , int > nxt[ MAXN ][ MAXN ] ;
ll dp[ MAXN ][ MAXN ] ;

void input ( ) {
    cin >> n >> a ;
    cin >> costA >> costB >> costC ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        for ( int j = i ; j <= n ; ++ j ) {
            aux[ i ][ j ] = aux[ i ][ j - 1 ] ;
            aux[ i ][ j ].l = i , aux[ i ][ j ].r = j ;
            aux[ i ][ j ].advance ( a[ j - 1 ] - 'a' + 1 ) ;
            srt.push_back ( aux[ i ][ j ] ) ;
        }
    }
    auto cmp = [ & ] ( hsh p1 , hsh p2 ) {
        for ( auto i : { 0 , 1 } ) {
            if ( p1.val[ i ] != p2.val[ i ] ) {
                return ( p1.val[ i ] < p2.val[ i ] ) ;
            }
        }
        return false ;
    };
    sort ( srt.begin ( ) , srt.end ( ) , cmp ) ;
    int sz = srt.size ( ) ;
    eq_class[ srt[ 0 ].l ][ srt[ 0 ].r ] = 1 ;
    int tp = 1 ;
    for ( int i = 1 ; i < sz ; ++ i ) {
        if ( srt[ i ].val[ 0 ] == srt[ i - 1 ].val[ 0 ] && srt[ i ].val[ 1 ] == srt[ i - 1 ].val[ 1 ] ) {
            eq_class[ srt[ i ].l ][ srt[ i ].r ] = tp ;
        }
        else {
            eq_class[ srt[ i ].l ][ srt[ i ].r ] = ++ tp ;
        }
    }
    for ( int i = 1 ; i <= n + 2 ; ++ i ) {
        for ( int j = i ; j <= n + 2 ; ++ j ) {
            class_list[ eq_class[ i ][ j ] ].push_back ( { i , j } ) ;
            dp[ i ][ j ] = 1e15 ;
        }
    }
    for ( int i = 1 ; i <= n + 2 ; ++ i ) {
        for ( int j = 0 ; j < i ; ++ j ) {
            dp[ i ][ j ] = costB ;
        }
    }
    for ( int wh = 1 ; wh <= tp ; ++ wh ) {
        int sz = class_list[ wh ].size ( ) ;
        int pos = 0 ;
        for ( int i = 0 ; i < sz ; ++ i ) {
            while ( pos < sz && class_list[ wh ][ i ].second >= class_list[ wh ][ pos ].first ) {
                ++ pos ;
            }
            if ( pos < sz ) {
                nxt[ class_list[ wh ][ i ].first ][ class_list[ wh ][ i ].second ] = class_list[ wh ][ pos ] ;
            }
            else {
                nxt[ class_list[ wh ][ i ].first ][ class_list[ wh ][ i ].second ] = { -1 , -1 } ;
            }
        }
    }
}

void solve ( ) {
    for ( int len = 0 ; len < n ; ++ len ) {
        for ( int i = 1 ; i + len <= n ; ++ i ) {
            int j = i + len ;
            dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i + 1 ][ j ] + costA ) ;
            dp[ i ][ j ] = min ( dp[ i ][ j ] , dp[ i ][ j - 1 ] + costA ) ;
            pair < int , int > act ;
            act = { i , j } ;
            int coef = 1 ;
            while ( 1 ) {
                act = nxt[ act.first ][ act.second ] ;
                if ( act.first < 0 ) { break ; }
                ++ coef ;
                int lft = ( act.second - i + 1 ) - coef * ( j - i + 1 ) ;
                dp[ i ][ act.second ] = min ( dp[ i ][ act.second ] , dp[ i ][ j ] + coef * costC + lft * costA + costB ) ;
            }
        }
    }
    cout << dp[ 1 ][ n ] - costB << "\n" ;
}


int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t ;
    /// cin >> t ;
    t = 1 ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 147900 KB Output is correct
2 Correct 71 ms 147848 KB Output is correct
3 Correct 73 ms 147896 KB Output is correct
4 Correct 78 ms 147924 KB Output is correct
5 Correct 74 ms 147928 KB Output is correct
6 Correct 69 ms 147896 KB Output is correct
7 Correct 68 ms 147936 KB Output is correct
8 Correct 67 ms 147944 KB Output is correct
9 Correct 76 ms 147968 KB Output is correct
10 Correct 77 ms 147932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 148128 KB Output is correct
2 Correct 71 ms 147968 KB Output is correct
3 Correct 646 ms 319036 KB Output is correct
4 Correct 732 ms 345692 KB Output is correct
5 Correct 936 ms 373296 KB Output is correct
6 Correct 1063 ms 404956 KB Output is correct
7 Correct 1262 ms 440528 KB Output is correct
8 Correct 1243 ms 440460 KB Output is correct
9 Correct 1263 ms 440492 KB Output is correct
10 Correct 76 ms 147944 KB Output is correct
11 Correct 73 ms 147960 KB Output is correct
12 Correct 74 ms 147916 KB Output is correct
13 Correct 75 ms 147916 KB Output is correct
14 Correct 84 ms 148024 KB Output is correct
15 Correct 90 ms 148152 KB Output is correct
16 Correct 80 ms 148456 KB Output is correct
17 Correct 75 ms 147900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 147900 KB Output is correct
2 Correct 71 ms 147848 KB Output is correct
3 Correct 73 ms 147896 KB Output is correct
4 Correct 78 ms 147924 KB Output is correct
5 Correct 74 ms 147928 KB Output is correct
6 Correct 69 ms 147896 KB Output is correct
7 Correct 68 ms 147936 KB Output is correct
8 Correct 67 ms 147944 KB Output is correct
9 Correct 76 ms 147968 KB Output is correct
10 Correct 77 ms 147932 KB Output is correct
11 Correct 76 ms 148092 KB Output is correct
12 Correct 78 ms 148208 KB Output is correct
13 Correct 77 ms 148172 KB Output is correct
14 Correct 83 ms 148056 KB Output is correct
15 Correct 74 ms 148260 KB Output is correct
16 Correct 74 ms 148052 KB Output is correct
17 Correct 77 ms 148108 KB Output is correct
18 Correct 75 ms 147916 KB Output is correct
19 Correct 89 ms 148044 KB Output is correct
20 Correct 73 ms 147912 KB Output is correct
21 Correct 89 ms 148368 KB Output is correct
22 Correct 77 ms 148424 KB Output is correct
23 Correct 80 ms 148428 KB Output is correct
24 Correct 75 ms 148364 KB Output is correct
25 Correct 93 ms 148444 KB Output is correct
26 Correct 92 ms 148408 KB Output is correct
27 Correct 75 ms 148436 KB Output is correct
28 Correct 80 ms 148352 KB Output is correct
29 Correct 77 ms 148452 KB Output is correct
30 Correct 81 ms 148460 KB Output is correct
31 Correct 82 ms 148472 KB Output is correct
32 Correct 81 ms 148408 KB Output is correct
33 Correct 74 ms 148428 KB Output is correct
34 Correct 74 ms 147908 KB Output is correct
35 Correct 78 ms 147960 KB Output is correct
36 Correct 74 ms 147980 KB Output is correct
37 Correct 75 ms 148012 KB Output is correct
38 Correct 80 ms 148136 KB Output is correct
39 Correct 94 ms 148396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 147900 KB Output is correct
2 Correct 71 ms 147848 KB Output is correct
3 Correct 73 ms 147896 KB Output is correct
4 Correct 78 ms 147924 KB Output is correct
5 Correct 74 ms 147928 KB Output is correct
6 Correct 69 ms 147896 KB Output is correct
7 Correct 68 ms 147936 KB Output is correct
8 Correct 67 ms 147944 KB Output is correct
9 Correct 76 ms 147968 KB Output is correct
10 Correct 77 ms 147932 KB Output is correct
11 Correct 76 ms 148092 KB Output is correct
12 Correct 78 ms 148208 KB Output is correct
13 Correct 77 ms 148172 KB Output is correct
14 Correct 83 ms 148056 KB Output is correct
15 Correct 74 ms 148260 KB Output is correct
16 Correct 74 ms 148052 KB Output is correct
17 Correct 77 ms 148108 KB Output is correct
18 Correct 75 ms 147916 KB Output is correct
19 Correct 89 ms 148044 KB Output is correct
20 Correct 73 ms 147912 KB Output is correct
21 Correct 89 ms 148368 KB Output is correct
22 Correct 77 ms 148424 KB Output is correct
23 Correct 80 ms 148428 KB Output is correct
24 Correct 75 ms 148364 KB Output is correct
25 Correct 93 ms 148444 KB Output is correct
26 Correct 92 ms 148408 KB Output is correct
27 Correct 75 ms 148436 KB Output is correct
28 Correct 80 ms 148352 KB Output is correct
29 Correct 77 ms 148452 KB Output is correct
30 Correct 81 ms 148460 KB Output is correct
31 Correct 82 ms 148472 KB Output is correct
32 Correct 81 ms 148408 KB Output is correct
33 Correct 74 ms 148428 KB Output is correct
34 Correct 74 ms 147908 KB Output is correct
35 Correct 78 ms 147960 KB Output is correct
36 Correct 74 ms 147980 KB Output is correct
37 Correct 75 ms 148012 KB Output is correct
38 Correct 80 ms 148136 KB Output is correct
39 Correct 94 ms 148396 KB Output is correct
40 Correct 75 ms 149660 KB Output is correct
41 Correct 79 ms 152872 KB Output is correct
42 Correct 79 ms 153164 KB Output is correct
43 Correct 78 ms 153196 KB Output is correct
44 Correct 82 ms 153248 KB Output is correct
45 Correct 102 ms 153204 KB Output is correct
46 Correct 86 ms 153240 KB Output is correct
47 Correct 78 ms 152868 KB Output is correct
48 Correct 80 ms 153008 KB Output is correct
49 Correct 80 ms 153032 KB Output is correct
50 Correct 86 ms 153032 KB Output is correct
51 Correct 88 ms 152984 KB Output is correct
52 Correct 88 ms 153060 KB Output is correct
53 Correct 80 ms 153208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 147900 KB Output is correct
2 Correct 71 ms 147848 KB Output is correct
3 Correct 73 ms 147896 KB Output is correct
4 Correct 78 ms 147924 KB Output is correct
5 Correct 74 ms 147928 KB Output is correct
6 Correct 69 ms 147896 KB Output is correct
7 Correct 68 ms 147936 KB Output is correct
8 Correct 67 ms 147944 KB Output is correct
9 Correct 76 ms 147968 KB Output is correct
10 Correct 77 ms 147932 KB Output is correct
11 Correct 76 ms 148092 KB Output is correct
12 Correct 78 ms 148208 KB Output is correct
13 Correct 77 ms 148172 KB Output is correct
14 Correct 83 ms 148056 KB Output is correct
15 Correct 74 ms 148260 KB Output is correct
16 Correct 74 ms 148052 KB Output is correct
17 Correct 77 ms 148108 KB Output is correct
18 Correct 75 ms 147916 KB Output is correct
19 Correct 89 ms 148044 KB Output is correct
20 Correct 73 ms 147912 KB Output is correct
21 Correct 89 ms 148368 KB Output is correct
22 Correct 77 ms 148424 KB Output is correct
23 Correct 80 ms 148428 KB Output is correct
24 Correct 75 ms 148364 KB Output is correct
25 Correct 93 ms 148444 KB Output is correct
26 Correct 92 ms 148408 KB Output is correct
27 Correct 75 ms 148436 KB Output is correct
28 Correct 80 ms 148352 KB Output is correct
29 Correct 77 ms 148452 KB Output is correct
30 Correct 81 ms 148460 KB Output is correct
31 Correct 82 ms 148472 KB Output is correct
32 Correct 81 ms 148408 KB Output is correct
33 Correct 74 ms 148428 KB Output is correct
34 Correct 74 ms 147908 KB Output is correct
35 Correct 78 ms 147960 KB Output is correct
36 Correct 74 ms 147980 KB Output is correct
37 Correct 75 ms 148012 KB Output is correct
38 Correct 80 ms 148136 KB Output is correct
39 Correct 94 ms 148396 KB Output is correct
40 Correct 75 ms 149660 KB Output is correct
41 Correct 79 ms 152872 KB Output is correct
42 Correct 79 ms 153164 KB Output is correct
43 Correct 78 ms 153196 KB Output is correct
44 Correct 82 ms 153248 KB Output is correct
45 Correct 102 ms 153204 KB Output is correct
46 Correct 86 ms 153240 KB Output is correct
47 Correct 78 ms 152868 KB Output is correct
48 Correct 80 ms 153008 KB Output is correct
49 Correct 80 ms 153032 KB Output is correct
50 Correct 86 ms 153032 KB Output is correct
51 Correct 88 ms 152984 KB Output is correct
52 Correct 88 ms 153060 KB Output is correct
53 Correct 80 ms 153208 KB Output is correct
54 Correct 107 ms 165608 KB Output is correct
55 Correct 203 ms 206588 KB Output is correct
56 Correct 313 ms 216688 KB Output is correct
57 Correct 305 ms 216864 KB Output is correct
58 Correct 295 ms 216936 KB Output is correct
59 Correct 311 ms 216884 KB Output is correct
60 Correct 312 ms 216952 KB Output is correct
61 Correct 250 ms 209572 KB Output is correct
62 Correct 200 ms 207212 KB Output is correct
63 Correct 316 ms 213660 KB Output is correct
64 Correct 275 ms 214092 KB Output is correct
65 Correct 269 ms 210736 KB Output is correct
66 Correct 284 ms 210720 KB Output is correct
67 Correct 298 ms 216992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 147900 KB Output is correct
2 Correct 71 ms 147848 KB Output is correct
3 Correct 73 ms 147896 KB Output is correct
4 Correct 78 ms 147924 KB Output is correct
5 Correct 74 ms 147928 KB Output is correct
6 Correct 69 ms 147896 KB Output is correct
7 Correct 68 ms 147936 KB Output is correct
8 Correct 67 ms 147944 KB Output is correct
9 Correct 76 ms 147968 KB Output is correct
10 Correct 77 ms 147932 KB Output is correct
11 Correct 77 ms 148128 KB Output is correct
12 Correct 71 ms 147968 KB Output is correct
13 Correct 646 ms 319036 KB Output is correct
14 Correct 732 ms 345692 KB Output is correct
15 Correct 936 ms 373296 KB Output is correct
16 Correct 1063 ms 404956 KB Output is correct
17 Correct 1262 ms 440528 KB Output is correct
18 Correct 1243 ms 440460 KB Output is correct
19 Correct 1263 ms 440492 KB Output is correct
20 Correct 76 ms 147944 KB Output is correct
21 Correct 73 ms 147960 KB Output is correct
22 Correct 74 ms 147916 KB Output is correct
23 Correct 75 ms 147916 KB Output is correct
24 Correct 84 ms 148024 KB Output is correct
25 Correct 90 ms 148152 KB Output is correct
26 Correct 80 ms 148456 KB Output is correct
27 Correct 75 ms 147900 KB Output is correct
28 Correct 76 ms 148092 KB Output is correct
29 Correct 78 ms 148208 KB Output is correct
30 Correct 77 ms 148172 KB Output is correct
31 Correct 83 ms 148056 KB Output is correct
32 Correct 74 ms 148260 KB Output is correct
33 Correct 74 ms 148052 KB Output is correct
34 Correct 77 ms 148108 KB Output is correct
35 Correct 75 ms 147916 KB Output is correct
36 Correct 89 ms 148044 KB Output is correct
37 Correct 73 ms 147912 KB Output is correct
38 Correct 89 ms 148368 KB Output is correct
39 Correct 77 ms 148424 KB Output is correct
40 Correct 80 ms 148428 KB Output is correct
41 Correct 75 ms 148364 KB Output is correct
42 Correct 93 ms 148444 KB Output is correct
43 Correct 92 ms 148408 KB Output is correct
44 Correct 75 ms 148436 KB Output is correct
45 Correct 80 ms 148352 KB Output is correct
46 Correct 77 ms 148452 KB Output is correct
47 Correct 81 ms 148460 KB Output is correct
48 Correct 82 ms 148472 KB Output is correct
49 Correct 81 ms 148408 KB Output is correct
50 Correct 74 ms 148428 KB Output is correct
51 Correct 74 ms 147908 KB Output is correct
52 Correct 78 ms 147960 KB Output is correct
53 Correct 74 ms 147980 KB Output is correct
54 Correct 75 ms 148012 KB Output is correct
55 Correct 80 ms 148136 KB Output is correct
56 Correct 94 ms 148396 KB Output is correct
57 Correct 75 ms 149660 KB Output is correct
58 Correct 79 ms 152872 KB Output is correct
59 Correct 79 ms 153164 KB Output is correct
60 Correct 78 ms 153196 KB Output is correct
61 Correct 82 ms 153248 KB Output is correct
62 Correct 102 ms 153204 KB Output is correct
63 Correct 86 ms 153240 KB Output is correct
64 Correct 78 ms 152868 KB Output is correct
65 Correct 80 ms 153008 KB Output is correct
66 Correct 80 ms 153032 KB Output is correct
67 Correct 86 ms 153032 KB Output is correct
68 Correct 88 ms 152984 KB Output is correct
69 Correct 88 ms 153060 KB Output is correct
70 Correct 80 ms 153208 KB Output is correct
71 Correct 107 ms 165608 KB Output is correct
72 Correct 203 ms 206588 KB Output is correct
73 Correct 313 ms 216688 KB Output is correct
74 Correct 305 ms 216864 KB Output is correct
75 Correct 295 ms 216936 KB Output is correct
76 Correct 311 ms 216884 KB Output is correct
77 Correct 312 ms 216952 KB Output is correct
78 Correct 250 ms 209572 KB Output is correct
79 Correct 200 ms 207212 KB Output is correct
80 Correct 316 ms 213660 KB Output is correct
81 Correct 275 ms 214092 KB Output is correct
82 Correct 269 ms 210736 KB Output is correct
83 Correct 284 ms 210720 KB Output is correct
84 Correct 298 ms 216992 KB Output is correct
85 Correct 779 ms 305212 KB Output is correct
86 Correct 2385 ms 504720 KB Output is correct
87 Correct 2359 ms 504936 KB Output is correct
88 Correct 2337 ms 505000 KB Output is correct
89 Correct 2334 ms 505148 KB Output is correct
90 Correct 2323 ms 505080 KB Output is correct
91 Correct 1185 ms 446036 KB Output is correct
92 Correct 1146 ms 444252 KB Output is correct
93 Correct 2032 ms 478416 KB Output is correct
94 Correct 2040 ms 488092 KB Output is correct
95 Correct 1877 ms 465676 KB Output is correct
96 Correct 1863 ms 465764 KB Output is correct
97 Correct 2301 ms 505316 KB Output is correct