Submission #963935

#TimeUsernameProblemLanguageResultExecution timeMemory
963935GrindMachineCopy and Paste 3 (JOI22_copypaste3)C++17
62 / 100
3098 ms254228 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
https://codeforces.com/blog/entry/101003?#comment-897398

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

vector<int> ps = {5791, 8237, 3067, 6991, 3527};
vector<int> mods = {791139137, 433567597, 271149607, 561259969, 222708581};
ll pows[N][2];

void precalc() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

    shuffle(all(ps), rng);
    shuffle(all(mods), rng);

    rep(j, 2) {
        pows[0][j] = 1;
        rep1(i, N - 1) {
            pows[i][j] = pows[i - 1][j] * ps[j] % mods[j];
        }
    }
}

void solve(int test_case)
{
    precalc();

    ll n; cin >> n;
    string s; cin >> s;
    s = "$" + s;
    ll A,B,C; cin >> A >> B >> C;

    map<array<ll,2>,vector<pll>> mp;

    rep1(i,n){
        array<ll,2> hsh = {0,0};
        for(int j = i; j <= n; ++j){
            rep(x,2){
                hsh[x] = (hsh[x]*ps[x]+s[j]-'a'+1)%mods[x];
            }

            mp[hsh].pb({i,j});
        }
    }

    ll nxt[n+5][n+5];
    memset(nxt,-1,sizeof nxt);

    for(auto [hsh,pos] : mp){
        ll ptr = 0;
        pos.pb({inf1,inf1});
        for(auto [i,j] : pos){
            if(i > n) break;
            while(pos[ptr].ff <= j){
                ptr++;
            }

            nxt[i][j] = pos[ptr].ff;
        }
    }

    ll dp[n+5][n+5];
    memset(dp,0x3f,sizeof dp);
    rep1(i,n){
        dp[i][i] = A;
    }

    for(int d = 1; d <= n; ++d){
        rep1(i,n){
            ll j = i+d-1;
            amin(dp[i][j],dp[i+1][j]+A);
            amin(dp[i][j],dp[i][j-1]+A);

            ll l = i, r = j;
            ll paste = 1;

            while(true){
                ll l2 = nxt[l][r];
                ll r2 = l2+j-i;
                l = l2, r = r2;
                if(l > n or l < 1) break;
                paste++;
                ll type = (r2-i+1)-(j-i+1)*paste;
                amin(dp[i][r2],dp[i][j]+B+paste*C+type*A);
            }
        }
    }

    ll ans = dp[1][n];
    cout << ans << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...