Submission #1059670

# Submission time Handle Problem Language Result Execution time Memory
1059670 2024-08-15T06:55:38 Z vjudge1 Copy and Paste 3 (JOI22_copypaste3) C++17
15 / 100
3000 ms 650616 KB
#include <bits/stdc++.h>

using namespace std;
    
typedef long long ll;
#define int ll
const int  N = 2500 + 2;
struct H{
    int p,mod;
    vector<ll> h,inv;
    ll binpow(ll a,ll b) {
        ll ret = 1,k = a;
        while(b) {
            if(b & 1) {
                ret *= k;
                ret %= mod;
            }
            k *= k;
            k%=mod;
            b /= 2;
        }
        return ret;
    }
    void init(string s) {
        int n = (int)s.size();
        h.resize((int)s.size());
        inv.resize((int)s.size());
        ll x = 1;
        for(int i = 0;i < n;i++) {
            h[i] = x * 1ll * (s[i] - 'a' + 1)%mod;
            if(i) h[i] += h[i - 1];
            if(h[i] >= mod) h[i] -= mod;
            inv[i] = binpow(x,mod-2);
            x = x * 1ll * p%mod;
        }
    }
    int get(int l,int r) {
        ll val = h[r];
        if(l) {
            val -= h[l - 1];
        }
        if(val < 0) val += mod;
        val = val * inv[l]%mod;
        return val;
    }

}xx,yy;
string s;
ll a,b,c,h[N],p = 167,inv[N];
map<pair<int,int>,ll> mem;
ll solve(int l,int r) {
    if(r == l) {
        return a;
    }
    pair<int,int> t = {xx.get(l,r),yy.get(l,r)};
    if(mem.count(t)) {
        return mem[t];
    }
    vector<pair<pair<int,int>,array<int,2>>> x;
    int mx = (r - l + 1) / 2;
    for(int i = l;i <= r;i++) {
        for(int j = i;j <= min(r,i+mx-1);j++) {
            pair<int,int> T = {xx.get(i,j),yy.get(i,j)};
            // if(j != r) continue;
            x.push_back({T,{i,j}});
        }
    }
    sort(x.begin(),x.end());
    ll res = min(solve(l,r-1) + a,(r - l + 1) * 1ll * a),mx_ = -1;
    int n = (r - l + 1);
    for(int i  = 0;i < (int)x.size();) {
        int j = i;
        while(j < (int)x.size() && x[i].first == x[j].first) {
            j++;
        }
        if(x[j - 1].second[1] != r) {
            i = j;
            continue;
        }
        int mn = -1,col = 0;
        for(int k = i;k < j;k++) {
            if(x[k].second[0] > mn) {
                mn = x[k].second[1];
                col++;
            }
        }
        int sz = (x[i].second[1] - x[i].second[0] + 1);
        // cout << l << ' ' << r << ' ' << sz << ' ' << col << '\n';
        if(col == 1) {
            i = j;
            continue;
        }
        assert(sz <= n / 2);
        if(sz * col < mx_) {
            i = j;
            continue;
        }
        mx_ = sz * col;
        ll val = solve(x[i].second[0],x[i].second[1]) + (n - sz * 1ll * col) * 1ll * a + (col - 1) * c + b + c;
        res = min(res,val);
        i = j;
    }
    return mem[t] = res;
}
int n;
void test() {
    cin >> n >> s >> a >> b >> c;
    yy.p = 37;yy.mod = 998244353;yy.init(s);
    xx.p = 167;xx.mod = (int)1e9 + 7;xx.init(s);
    // cout << xx.get(1,2) << ' ' << yy.get(1,2) << ' ' << xx.get(4,5) << ' ' << yy.get(4,5) << '\n';
    cout << solve(0,n-1);
}
signed main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1; 
    // cin >> t;
    while(t--) {
        test();
    }
}  
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Execution timed out 3101 ms 650616 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 600 KB Output is correct
40 Correct 6 ms 3060 KB Output is correct
41 Correct 129 ms 32716 KB Output is correct
42 Correct 90 ms 32656 KB Output is correct
43 Correct 104 ms 32500 KB Output is correct
44 Correct 91 ms 32560 KB Output is correct
45 Correct 94 ms 32520 KB Output is correct
46 Correct 90 ms 32504 KB Output is correct
47 Correct 217 ms 33072 KB Output is correct
48 Correct 287 ms 32976 KB Output is correct
49 Correct 168 ms 32716 KB Output is correct
50 Correct 155 ms 32560 KB Output is correct
51 Correct 297 ms 33996 KB Output is correct
52 Correct 297 ms 33532 KB Output is correct
53 Incorrect 89 ms 32588 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 456 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 460 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 600 KB Output is correct
40 Correct 6 ms 3060 KB Output is correct
41 Correct 129 ms 32716 KB Output is correct
42 Correct 90 ms 32656 KB Output is correct
43 Correct 104 ms 32500 KB Output is correct
44 Correct 91 ms 32560 KB Output is correct
45 Correct 94 ms 32520 KB Output is correct
46 Correct 90 ms 32504 KB Output is correct
47 Correct 217 ms 33072 KB Output is correct
48 Correct 287 ms 32976 KB Output is correct
49 Correct 168 ms 32716 KB Output is correct
50 Correct 155 ms 32560 KB Output is correct
51 Correct 297 ms 33996 KB Output is correct
52 Correct 297 ms 33532 KB Output is correct
53 Incorrect 89 ms 32588 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 456 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 376 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Execution timed out 3101 ms 650616 KB Time limit exceeded
14 Halted 0 ms 0 KB -