Submission #850892

# Submission time Handle Problem Language Result Execution time Memory
850892 2023-09-17T16:37:17 Z danikoynov Chorus (JOI23_chorus) C++14
61 / 100
483 ms 414540 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll maxn = 5100;
ll n, k;
string s;

ll dp[maxn][maxn], pos[2][maxn], pref[maxn];
/**

*/

void chmin(ll &var, ll val) /// careful for long long
{
    var = min(var, val);
}

ll costd[maxn][maxn];

void precalc()
{
    for (ll j = 1; j <= n; j ++)
    {
        ll tk = 0;
        for (ll fix = 1; fix <= n - j; fix ++)
        {
                if (pos[0][fix + j] > j * 2 + fix)
                    tk += pos[0][fix + j] - (j * 2 + fix);
            costd[j + 1][j + fix] = tk;
        }
    }
}

ll cost(ll left, ll right)
{
    ll above = (left - 1) * 2;
    ll lf = left, rf = right;
    while(lf <= rf)
    {
        ll mf = (lf + rf) / 2;
        if (pos[0][mf] > above + (mf - left + 1))
            rf = mf - 1;
        else
            lf = mf + 1;
    }

    ll sum = pref[right] - pref[lf - 1];
    sum = sum - (right - lf + 1) * above;
    ll len = right - left + 1;
    ll sm = lf - left;
    sum = sum - (len) * (len + 1) / 2;
    sum = sum + (sm) * (sm + 1) / 2;
    /**for (ll i = lf; i <= right; i ++)
    {

            sum = sum + pos[0][i] - (above + (i - left + 1));
    }*/
    return sum;

}

void divide(ll row, ll left, ll right, ll optl, ll optr)
{
    if (left > right)
        return;

    ll mid = (left + right) / 2;
    ll opt = -1;
    for (ll j = optl; j <= min(optr, mid); j ++)
    {
        ll cur =dp[j - 1][row - 1] + costd[j][mid];
        if (cur < dp[mid][row])
        {
            dp[mid][row] = cur;
            opt = j;
        }
    }

    divide(row, left, mid - 1, optl, opt);
    divide(row, mid + 1, right, opt, optr);
}
void solve()
{
    cin >> n >> k >> s;

    s = "#" + s;
    ll cntA = 0, cntB = 0;
    for (ll i = 1; i <= 2 * n; i ++)
    {
        if (s[i] == 'A')
        {
            cntA ++;
            pos[0][cntA] = i;
            pref[cntA] = pref[cntA - 1] + i;
        }
        else
        {
            cntB ++;
            pos[1][cntB] = i;
        }
    }

    for (ll i = 0; i <= n; i ++)
        for (ll j = 0; j <= n; j ++)
        dp[i][j] = 4 * n * n; /// careful for overflow

 precalc();

        ll cr = 0;
    for (ll j = 1; j <= n; j ++)
    {
        cr = cr + pos[0][j] - j;

        dp[j][1] = cr;
    }

    for (ll j = 2; j <= k; j ++)

    {
        divide(j, j, n, j, n);
    }
    /**for (ll i = 1; i <= n; i ++)
        for (ll j = 2; j <= k; j ++)
    {
        for (ll fix = 1; fix <= i; fix ++)
        {
            if (dp[i - fix][j - 1] != 4 * n * n)
            {
                dp[i][j] = min(dp[i][j], dp[i - fix][j - 1] + cost(i - fix + 1, i));
            }
        }
    }*/


    ll ans = 4 * n * n;
    for (ll i = 1; i <= k; i ++)
        ans = min(ans, dp[n][i]);
    cout << ans << endl;
}

int main()
{
    speed();
    ///freopen("test.txt", "r", stdin);
    solve();
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 0 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 0 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 6 ms 39768 KB Output is correct
19 Correct 7 ms 39768 KB Output is correct
20 Correct 5 ms 41304 KB Output is correct
21 Correct 5 ms 41304 KB Output is correct
22 Correct 6 ms 41304 KB Output is correct
23 Correct 7 ms 41304 KB Output is correct
24 Correct 5 ms 39768 KB Output is correct
25 Correct 6 ms 39772 KB Output is correct
26 Correct 6 ms 39768 KB Output is correct
27 Correct 7 ms 39768 KB Output is correct
28 Correct 6 ms 39768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 0 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 6 ms 39768 KB Output is correct
19 Correct 7 ms 39768 KB Output is correct
20 Correct 5 ms 41304 KB Output is correct
21 Correct 5 ms 41304 KB Output is correct
22 Correct 6 ms 41304 KB Output is correct
23 Correct 7 ms 41304 KB Output is correct
24 Correct 5 ms 39768 KB Output is correct
25 Correct 6 ms 39772 KB Output is correct
26 Correct 6 ms 39768 KB Output is correct
27 Correct 7 ms 39768 KB Output is correct
28 Correct 6 ms 39768 KB Output is correct
29 Correct 266 ms 394188 KB Output is correct
30 Correct 483 ms 402480 KB Output is correct
31 Correct 226 ms 402256 KB Output is correct
32 Correct 64 ms 402508 KB Output is correct
33 Correct 64 ms 402680 KB Output is correct
34 Correct 353 ms 402756 KB Output is correct
35 Correct 345 ms 402512 KB Output is correct
36 Correct 85 ms 402256 KB Output is correct
37 Correct 233 ms 402256 KB Output is correct
38 Correct 321 ms 402480 KB Output is correct
39 Correct 357 ms 402484 KB Output is correct
40 Correct 236 ms 402488 KB Output is correct
41 Correct 232 ms 402500 KB Output is correct
42 Correct 282 ms 402760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 0 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 6 ms 39768 KB Output is correct
19 Correct 7 ms 39768 KB Output is correct
20 Correct 5 ms 41304 KB Output is correct
21 Correct 5 ms 41304 KB Output is correct
22 Correct 6 ms 41304 KB Output is correct
23 Correct 7 ms 41304 KB Output is correct
24 Correct 5 ms 39768 KB Output is correct
25 Correct 6 ms 39772 KB Output is correct
26 Correct 6 ms 39768 KB Output is correct
27 Correct 7 ms 39768 KB Output is correct
28 Correct 6 ms 39768 KB Output is correct
29 Correct 266 ms 394188 KB Output is correct
30 Correct 483 ms 402480 KB Output is correct
31 Correct 226 ms 402256 KB Output is correct
32 Correct 64 ms 402508 KB Output is correct
33 Correct 64 ms 402680 KB Output is correct
34 Correct 353 ms 402756 KB Output is correct
35 Correct 345 ms 402512 KB Output is correct
36 Correct 85 ms 402256 KB Output is correct
37 Correct 233 ms 402256 KB Output is correct
38 Correct 321 ms 402480 KB Output is correct
39 Correct 357 ms 402484 KB Output is correct
40 Correct 236 ms 402488 KB Output is correct
41 Correct 232 ms 402500 KB Output is correct
42 Correct 282 ms 402760 KB Output is correct
43 Runtime error 362 ms 414540 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 0 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2396 KB Output is correct
7 Correct 1 ms 4440 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 1 ms 4440 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 0 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 6 ms 39768 KB Output is correct
19 Correct 7 ms 39768 KB Output is correct
20 Correct 5 ms 41304 KB Output is correct
21 Correct 5 ms 41304 KB Output is correct
22 Correct 6 ms 41304 KB Output is correct
23 Correct 7 ms 41304 KB Output is correct
24 Correct 5 ms 39768 KB Output is correct
25 Correct 6 ms 39772 KB Output is correct
26 Correct 6 ms 39768 KB Output is correct
27 Correct 7 ms 39768 KB Output is correct
28 Correct 6 ms 39768 KB Output is correct
29 Correct 266 ms 394188 KB Output is correct
30 Correct 483 ms 402480 KB Output is correct
31 Correct 226 ms 402256 KB Output is correct
32 Correct 64 ms 402508 KB Output is correct
33 Correct 64 ms 402680 KB Output is correct
34 Correct 353 ms 402756 KB Output is correct
35 Correct 345 ms 402512 KB Output is correct
36 Correct 85 ms 402256 KB Output is correct
37 Correct 233 ms 402256 KB Output is correct
38 Correct 321 ms 402480 KB Output is correct
39 Correct 357 ms 402484 KB Output is correct
40 Correct 236 ms 402488 KB Output is correct
41 Correct 232 ms 402500 KB Output is correct
42 Correct 282 ms 402760 KB Output is correct
43 Runtime error 362 ms 414540 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -