Submission #872668

# Submission time Handle Problem Language Result Execution time Memory
872668 2023-11-13T14:05:31 Z winter0101 Copy and Paste 3 (JOI22_copypaste3) C++14
100 / 100
759 ms 73044 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=2509;
int n;
string s;
long long x,y,z;
long long f[maxn][maxn];
int lcp[maxn][maxn];
int id[maxn];
int nxt[maxn];
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("temp.INP","r",stdin);
    //freopen("temp.OUT","w",stdout);
    cin>>n>>s>>x>>y>>z;
    s=" "+s;
    for1(i,1,n){
    for1(j,1,n){
    lcp[i][j]=lcp[i-1][j-1]-1;
    if (lcp[i-1][j-1]==0)lcp[i][j]=0;
    while (lcp[i][j]<=n-max(j,i)+1&&s[i+lcp[i][j]]==s[j+lcp[i][j]]){
    lcp[i][j]++;
    }
    }
    }
    for1(i,1,n){
    for1(j,i,n){
    f[i][j]=1ll*(j-i+1)*x;
    }
    }
    for1(len,1,n){
    for1(i,1,n-len+1){
    id[i]=i;
    int j=i+len-1;
    if (i>1)f[i-1][j]=min(f[i-1][j],f[i][j]+x);
    if (j+1<=n)f[i][j+1]=min(f[i][j+1],f[i][j]+x);
    }
    sort(id+1,id+1+n-len+1,[&](int i,int j){
    if (lcp[i][j]>=len)return i<j;
    return s[i+lcp[i][j]]<s[j+lcp[i][j]];
    });
    for1(i,1,n-len+1)nxt[id[i]]=-1;
    //if (len==4)for1(i,1,n-len+1)cout<<id[i]<<" ";
    for1(i,1,n-len+1){
    int p=id[i];
    int poi=i;
    for1(j,i,n-len+1){
    int q=id[j];
    if (lcp[p][q]<len)break;
    poi=j;
    }
    //if (len==1)cout<<i<<" "<<poi<<'\n';
    for2(j,poi,i){
    int l=j+1,r=poi;
    //if (len==1)cout<<id[j]<<" "<<j<<" "<<l<<" "<<r<<'\n';
    nxt[id[j]]=-1;
    while (l<=r){
    int mid=(l+r)/2;
    if (id[j]+len<=id[mid]){
    nxt[id[j]]=mid;
    r=mid-1;
    }
    else l=mid+1;
    }
    //if (len==4&&id[j]==4)cout<<nxt[id[j]]<<'\n';
    long long cost=y+z+f[id[j]][id[j]+len-1];
    int nw=id[j];
    //if (len==1)cout<<id[j]<<" "<<nxt[id[j]]<<'\n';
    while (true){
    if (nxt[nw]==-1)break;
    //if (id[j]==1&&len==1)cout<<id[nw]<<" "<<id[nxt[nw]]<<'\n';
    int vcl=id[nxt[nw]];
    //if (len==4&&id[j]==4)cout<<id[j]<<" "<<nw<<" "<<vcl<<'\n';
    cost+=(vcl-1-(nw+len)+1)*x+z;
    nw=id[nxt[nw]];
    f[id[j]][nw+len-1]=min(f[id[j]][nw+len-1],cost);
    }
    }
    i=poi;
    }
    }
    cout<<f[1][n];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2512 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 223 ms 58060 KB Output is correct
4 Correct 232 ms 62172 KB Output is correct
5 Correct 275 ms 68188 KB Output is correct
6 Correct 318 ms 72432 KB Output is correct
7 Correct 429 ms 73044 KB Output is correct
8 Correct 378 ms 72784 KB Output is correct
9 Correct 402 ms 72832 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 0 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 0 ms 2512 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4568 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 0 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2512 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 6608 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6676 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6616 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 1 ms 4440 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2512 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 6608 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6676 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6616 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 1 ms 4440 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 3 ms 8536 KB Output is correct
43 Correct 4 ms 8540 KB Output is correct
44 Correct 3 ms 8732 KB Output is correct
45 Correct 3 ms 8540 KB Output is correct
46 Correct 3 ms 8540 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 3 ms 8536 KB Output is correct
49 Correct 3 ms 8544 KB Output is correct
50 Correct 3 ms 8544 KB Output is correct
51 Correct 3 ms 8732 KB Output is correct
52 Correct 3 ms 8588 KB Output is correct
53 Correct 3 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2512 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 4560 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 6608 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6676 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 1 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 6616 KB Output is correct
32 Correct 1 ms 6492 KB Output is correct
33 Correct 1 ms 6492 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 1 ms 4440 KB Output is correct
38 Correct 1 ms 4444 KB Output is correct
39 Correct 1 ms 6492 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 2 ms 8540 KB Output is correct
42 Correct 3 ms 8536 KB Output is correct
43 Correct 4 ms 8540 KB Output is correct
44 Correct 3 ms 8732 KB Output is correct
45 Correct 3 ms 8540 KB Output is correct
46 Correct 3 ms 8540 KB Output is correct
47 Correct 3 ms 8540 KB Output is correct
48 Correct 3 ms 8536 KB Output is correct
49 Correct 3 ms 8544 KB Output is correct
50 Correct 3 ms 8544 KB Output is correct
51 Correct 3 ms 8732 KB Output is correct
52 Correct 3 ms 8588 KB Output is correct
53 Correct 3 ms 8536 KB Output is correct
54 Correct 14 ms 18780 KB Output is correct
55 Correct 38 ms 33372 KB Output is correct
56 Correct 80 ms 33620 KB Output is correct
57 Correct 74 ms 33372 KB Output is correct
58 Correct 72 ms 33408 KB Output is correct
59 Correct 70 ms 33372 KB Output is correct
60 Correct 69 ms 33412 KB Output is correct
61 Correct 73 ms 33404 KB Output is correct
62 Correct 50 ms 33408 KB Output is correct
63 Correct 82 ms 33408 KB Output is correct
64 Correct 71 ms 33368 KB Output is correct
65 Correct 75 ms 33404 KB Output is correct
66 Correct 73 ms 33368 KB Output is correct
67 Correct 70 ms 33372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 0 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2512 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 223 ms 58060 KB Output is correct
14 Correct 232 ms 62172 KB Output is correct
15 Correct 275 ms 68188 KB Output is correct
16 Correct 318 ms 72432 KB Output is correct
17 Correct 429 ms 73044 KB Output is correct
18 Correct 378 ms 72784 KB Output is correct
19 Correct 402 ms 72832 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 0 ms 2392 KB Output is correct
22 Correct 0 ms 2396 KB Output is correct
23 Correct 0 ms 2512 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4568 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 0 ms 2396 KB Output is correct
28 Correct 1 ms 4560 KB Output is correct
29 Correct 1 ms 6492 KB Output is correct
30 Correct 1 ms 6492 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 6608 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 1 ms 2392 KB Output is correct
35 Correct 1 ms 2396 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 0 ms 2396 KB Output is correct
38 Correct 1 ms 6492 KB Output is correct
39 Correct 1 ms 6676 KB Output is correct
40 Correct 1 ms 6492 KB Output is correct
41 Correct 1 ms 6492 KB Output is correct
42 Correct 1 ms 6492 KB Output is correct
43 Correct 1 ms 6492 KB Output is correct
44 Correct 1 ms 6492 KB Output is correct
45 Correct 1 ms 6492 KB Output is correct
46 Correct 1 ms 6492 KB Output is correct
47 Correct 1 ms 6492 KB Output is correct
48 Correct 1 ms 6616 KB Output is correct
49 Correct 1 ms 6492 KB Output is correct
50 Correct 1 ms 6492 KB Output is correct
51 Correct 1 ms 2396 KB Output is correct
52 Correct 1 ms 2396 KB Output is correct
53 Correct 1 ms 2392 KB Output is correct
54 Correct 1 ms 4440 KB Output is correct
55 Correct 1 ms 4444 KB Output is correct
56 Correct 1 ms 6492 KB Output is correct
57 Correct 1 ms 6492 KB Output is correct
58 Correct 2 ms 8540 KB Output is correct
59 Correct 3 ms 8536 KB Output is correct
60 Correct 4 ms 8540 KB Output is correct
61 Correct 3 ms 8732 KB Output is correct
62 Correct 3 ms 8540 KB Output is correct
63 Correct 3 ms 8540 KB Output is correct
64 Correct 3 ms 8540 KB Output is correct
65 Correct 3 ms 8536 KB Output is correct
66 Correct 3 ms 8544 KB Output is correct
67 Correct 3 ms 8544 KB Output is correct
68 Correct 3 ms 8732 KB Output is correct
69 Correct 3 ms 8588 KB Output is correct
70 Correct 3 ms 8536 KB Output is correct
71 Correct 14 ms 18780 KB Output is correct
72 Correct 38 ms 33372 KB Output is correct
73 Correct 80 ms 33620 KB Output is correct
74 Correct 74 ms 33372 KB Output is correct
75 Correct 72 ms 33408 KB Output is correct
76 Correct 70 ms 33372 KB Output is correct
77 Correct 69 ms 33412 KB Output is correct
78 Correct 73 ms 33404 KB Output is correct
79 Correct 50 ms 33408 KB Output is correct
80 Correct 82 ms 33408 KB Output is correct
81 Correct 71 ms 33368 KB Output is correct
82 Correct 75 ms 33404 KB Output is correct
83 Correct 73 ms 33368 KB Output is correct
84 Correct 70 ms 33372 KB Output is correct
85 Correct 217 ms 51912 KB Output is correct
86 Correct 620 ms 72828 KB Output is correct
87 Correct 668 ms 72836 KB Output is correct
88 Correct 631 ms 72792 KB Output is correct
89 Correct 625 ms 72836 KB Output is correct
90 Correct 582 ms 73044 KB Output is correct
91 Correct 708 ms 73044 KB Output is correct
92 Correct 653 ms 72836 KB Output is correct
93 Correct 634 ms 72784 KB Output is correct
94 Correct 688 ms 73044 KB Output is correct
95 Correct 759 ms 72836 KB Output is correct
96 Correct 744 ms 72832 KB Output is correct
97 Correct 611 ms 73044 KB Output is correct