Submission #850891

# Submission time Handle Problem Language Result Execution time Memory
850891 2023-09-17T16:36:10 Z danikoynov Chorus (JOI23_chorus) C++14
61 / 100
2800 ms 207868 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 int maxn = 5100;
int n, k;
string s;

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

*/

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

///int cost[maxn][maxn];

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

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

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

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

}

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

    int mid = (left + right) / 2;
    int opt = -1;
    for (int j = optl; j <= min(optr, mid); j ++)
    {
        int cur =dp[j - 1][row - 1] + cost(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;
    int cntA = 0, cntB = 0;
    for (int 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 (int i = 0; i <= n; i ++)
        for (int j = 0; j <= n; j ++)
        dp[i][j] = 4 * n * n; /// careful for overflow

 precalc();

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

        dp[j][1] = cr;
    }

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

    {
        divide(j, j, n, j, n);
    }
    /**for (int i = 1; i <= n; i ++)
        for (int j = 2; j <= k; j ++)
    {
        for (int 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));
            }
        }
    }*/


    int ans = 4 * n * n;
    for (int 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 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 8 ms 10584 KB Output is correct
19 Correct 15 ms 10584 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10584 KB Output is correct
22 Correct 8 ms 10584 KB Output is correct
23 Correct 8 ms 10584 KB Output is correct
24 Correct 3 ms 10584 KB Output is correct
25 Correct 9 ms 10832 KB Output is correct
26 Correct 9 ms 10584 KB Output is correct
27 Correct 6 ms 10584 KB Output is correct
28 Correct 6 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 8 ms 10584 KB Output is correct
19 Correct 15 ms 10584 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10584 KB Output is correct
22 Correct 8 ms 10584 KB Output is correct
23 Correct 8 ms 10584 KB Output is correct
24 Correct 3 ms 10584 KB Output is correct
25 Correct 9 ms 10832 KB Output is correct
26 Correct 9 ms 10584 KB Output is correct
27 Correct 6 ms 10584 KB Output is correct
28 Correct 6 ms 10588 KB Output is correct
29 Correct 869 ms 99152 KB Output is correct
30 Correct 2800 ms 101204 KB Output is correct
31 Correct 1143 ms 101208 KB Output is correct
32 Correct 18 ms 101208 KB Output is correct
33 Correct 19 ms 101200 KB Output is correct
34 Correct 937 ms 101208 KB Output is correct
35 Correct 934 ms 101200 KB Output is correct
36 Correct 37 ms 101208 KB Output is correct
37 Correct 889 ms 101204 KB Output is correct
38 Correct 1057 ms 101208 KB Output is correct
39 Correct 1327 ms 101208 KB Output is correct
40 Correct 687 ms 101204 KB Output is correct
41 Correct 679 ms 101208 KB Output is correct
42 Correct 1500 ms 101208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 8 ms 10584 KB Output is correct
19 Correct 15 ms 10584 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10584 KB Output is correct
22 Correct 8 ms 10584 KB Output is correct
23 Correct 8 ms 10584 KB Output is correct
24 Correct 3 ms 10584 KB Output is correct
25 Correct 9 ms 10832 KB Output is correct
26 Correct 9 ms 10584 KB Output is correct
27 Correct 6 ms 10584 KB Output is correct
28 Correct 6 ms 10588 KB Output is correct
29 Correct 869 ms 99152 KB Output is correct
30 Correct 2800 ms 101204 KB Output is correct
31 Correct 1143 ms 101208 KB Output is correct
32 Correct 18 ms 101208 KB Output is correct
33 Correct 19 ms 101200 KB Output is correct
34 Correct 937 ms 101208 KB Output is correct
35 Correct 934 ms 101200 KB Output is correct
36 Correct 37 ms 101208 KB Output is correct
37 Correct 889 ms 101204 KB Output is correct
38 Correct 1057 ms 101208 KB Output is correct
39 Correct 1327 ms 101208 KB Output is correct
40 Correct 687 ms 101204 KB Output is correct
41 Correct 679 ms 101208 KB Output is correct
42 Correct 1500 ms 101208 KB Output is correct
43 Runtime error 197 ms 207868 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 0 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 0 ms 2396 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 0 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 2 ms 4440 KB Output is correct
18 Correct 8 ms 10584 KB Output is correct
19 Correct 15 ms 10584 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10584 KB Output is correct
22 Correct 8 ms 10584 KB Output is correct
23 Correct 8 ms 10584 KB Output is correct
24 Correct 3 ms 10584 KB Output is correct
25 Correct 9 ms 10832 KB Output is correct
26 Correct 9 ms 10584 KB Output is correct
27 Correct 6 ms 10584 KB Output is correct
28 Correct 6 ms 10588 KB Output is correct
29 Correct 869 ms 99152 KB Output is correct
30 Correct 2800 ms 101204 KB Output is correct
31 Correct 1143 ms 101208 KB Output is correct
32 Correct 18 ms 101208 KB Output is correct
33 Correct 19 ms 101200 KB Output is correct
34 Correct 937 ms 101208 KB Output is correct
35 Correct 934 ms 101200 KB Output is correct
36 Correct 37 ms 101208 KB Output is correct
37 Correct 889 ms 101204 KB Output is correct
38 Correct 1057 ms 101208 KB Output is correct
39 Correct 1327 ms 101208 KB Output is correct
40 Correct 687 ms 101204 KB Output is correct
41 Correct 679 ms 101208 KB Output is correct
42 Correct 1500 ms 101208 KB Output is correct
43 Runtime error 197 ms 207868 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -