Submission #549863

# Submission time Handle Problem Language Result Execution time Memory
549863 2022-04-16T17:16:05 Z BalintR Copy and Paste 3 (JOI22_copypaste3) C++17
25 / 100
3000 ms 8220 KB
#include <bits/stdc++.h>
using namespace std;

typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cmplx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define boost() cin.sync_with_stdio(0); cin.tie(0)
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound((v).begin(), (v).end(), x) - (v).begin())
#define ubv(v, x) (upper_bound((v).begin(), (v).end(), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cout << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cout << #arr; FR(_i, n) cout << ' ' << arr[_i]; cout << endl;}

const int MOD = 1e9 + 7;
const int X = 30;
const int MN = 1005;
int n;
string str;
int aCost, bCost, cCost;
ll pws[MN], hashes[MN];
ll dp[MN][MN];
int len;
vector<pll> vec;

void proc(int l, int r){
    vi smallVec;
    ll minCost = 0;

    FOR(i, l, r){
        int a = vec[i].sn;
        smallVec.pb(a);
        minCost = max(minCost, dp[a][a+len]);
    }
    minCost = (ll) aCost*len - minCost + bCost;

    FR(i, n){
        int pos = i;
        int cur = 0;
        while(true){
            int nxt = lbv(smallVec, pos);
            if(nxt == SZ(smallVec)) break;
            pos = smallVec[nxt] + len;
            cur++;
            assert(pos <= n);
            dp[i][pos] = max(dp[i][pos], (ll) cur*len*aCost - minCost - (ll) cur*cCost);
        }
    }
}

int main(){
    boost();
    cin >> n >> str >> aCost >> bCost >> cCost;
    for(char &ch : str) ch -= 'a';
    pws[0] = 1;
    FR(i, n) pws[i+1] = pws[i] * X % MOD;
    FR(i, n) hashes[i+1] = (hashes[i]*X + str[i]) % MOD;

    //dbgArr(pws, n+1);
    //dbgArr(hashes, n+1);

    for(len = 1; len <= n; len++){
        FR(i, n) FR(j, n) dp[i][j+1] = max(dp[i][j+1], dp[i][j]);

        int num = n-len+1;
        vec.clear();

        FR(i, num){
            ll hash = (hashes[i+len] - hashes[i]*pws[len]) % MOD;
            if(hash < 0) hash += MOD;
            vec.pb({hash, i});
        }

        sort(ALL(vec));
        int l = 0;
        FOR(r, 1, num+1){
            if(r == num || vec[r].fs != vec[r-1].fs){
                proc(l, r);
                l = r;
            }
        }
    }

    ll ans = (ll) n*aCost - dp[0][n];
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 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 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Runtime error 1 ms 468 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 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 328 KB Output is correct
10 Correct 1 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 1 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 328 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 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 456 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 452 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 444 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 456 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
36 Correct 0 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 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 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 328 KB Output is correct
10 Correct 1 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 1 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 328 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 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 456 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 452 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 444 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 456 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
36 Correct 0 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 340 KB Output is correct
40 Correct 3 ms 724 KB Output is correct
41 Correct 15 ms 1452 KB Output is correct
42 Correct 30 ms 1364 KB Output is correct
43 Correct 29 ms 1364 KB Output is correct
44 Correct 30 ms 1456 KB Output is correct
45 Correct 29 ms 1364 KB Output is correct
46 Correct 34 ms 1364 KB Output is correct
47 Correct 24 ms 1364 KB Output is correct
48 Correct 31 ms 1364 KB Output is correct
49 Correct 34 ms 1436 KB Output is correct
50 Correct 32 ms 1364 KB Output is correct
51 Correct 28 ms 1364 KB Output is correct
52 Correct 28 ms 1436 KB Output is correct
53 Correct 30 ms 1448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 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 328 KB Output is correct
10 Correct 1 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 1 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 328 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 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 456 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 452 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 444 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 456 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 324 KB Output is correct
36 Correct 0 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 340 KB Output is correct
40 Correct 3 ms 724 KB Output is correct
41 Correct 15 ms 1452 KB Output is correct
42 Correct 30 ms 1364 KB Output is correct
43 Correct 29 ms 1364 KB Output is correct
44 Correct 30 ms 1456 KB Output is correct
45 Correct 29 ms 1364 KB Output is correct
46 Correct 34 ms 1364 KB Output is correct
47 Correct 24 ms 1364 KB Output is correct
48 Correct 31 ms 1364 KB Output is correct
49 Correct 34 ms 1436 KB Output is correct
50 Correct 32 ms 1364 KB Output is correct
51 Correct 28 ms 1364 KB Output is correct
52 Correct 28 ms 1436 KB Output is correct
53 Correct 30 ms 1448 KB Output is correct
54 Correct 367 ms 3684 KB Output is correct
55 Correct 1331 ms 8220 KB Output is correct
56 Execution timed out 3036 ms 8148 KB Time limit exceeded
57 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 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 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Runtime error 1 ms 468 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -