Submission #852116

# Submission time Handle Problem Language Result Execution time Memory
852116 2023-09-21T09:11:44 Z danikoynov Chorus (JOI23_chorus) C++14
87 / 100
7000 ms 40504 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 = 1e6 + 10;
const ll inf = 1e18;

ll n, k;
string s;

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

*/

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

int pre_cost[maxn];

void precompute()
{
    for (int i = 1; i <= n; i ++)
    {
        int above = (i - 1) * 2;
        int lf = i, rf = n;
        while(lf <= rf)
        {
            int mf = (lf + rf) / 2;
            if (pos[0][mf] > above + (mf - i + 1))
                rf = mf - 1;
            else
                lf = mf + 1;
        }
        pre_cost[i] = lf;
    }
}
ll cost(ll left, ll right)
{
    if (left > right)
        return 0;
    if (pre_cost[left] > right)
        return 0;

        ll lf = pre_cost[left];
    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);
}*/



ll dp[maxn], f[maxn];

ll get_better(int i, int j)
{
    int lf = 1, rf = n;
    while(lf <= rf)
    {
        int mf = (lf + rf) / 2;
        if (dp[i - 1] + cost(i, mf) > dp[j - 1] + cost(j, mf))
            rf = mf - 1;
        else
            lf = mf + 1;
    }

    return lf;
}
pair < ll, ll > calc_dp(ll group_cost)
{
    for (int i = 1; i <= n; i ++)
    {
        dp[i] = inf;
        f[i] = 0;
    }
    deque < int > dq;
    for (int i = 1; i <= n; i ++)
    {
        while(dq.size() > 1)
        {
            int last = dq.back();
            int bef = dq[dq.size() - 2];
            if (get_better(last, i) < get_better(bef, last))
            {
                dq.pop_back();
            }
            else
            {
                break;
            }
        }
        dq.push_back(i);

        while(dq.size() > 1)
        {
            int fs = dq[0];
            int sc = dq[1];
            if (get_better(fs, sc) <= i)
                dq.pop_front();
            else
                break;
        }

        dp[i] = dp[dq[0] - 1] + cost(dq[0], i) + group_cost;
        f[i] = f[dq[0] - 1] + 1;

        /**for (int j = 1; j <= i; j ++)
        {
            ll cur = dp[j - 1] + cost(j, i) + group_cost;
            if (cur < dp[i])
            {
                dp[i] = cur;
                f[i] = f[j - 1] + 1;
            }
        }*/
    }


    return {dp[n], f[n]};
}
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

    /**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);
    }*/

    precompute();
    ll lf = 0, rf = n * n / 2;
    while(lf <= rf)
    {
        ll mf = (lf + rf) / 2;
        pair < ll, ll > cur = calc_dp(mf);
        if (cur.second <= k)
            rf = mf - 1;
        else
            lf = mf + 1;
    }

    if (rf < 0)
    {
        pair < ll, ll > cur = calc_dp(0);
        cout << cur.first << endl;
        return;
    }
    pair < ll, ll > fs = calc_dp(rf);
    pair < ll, ll > ds = calc_dp(rf + 1);

    ll fs_val = fs.first - fs.second * rf;
    ll ds_val = ds.first - ds.second * (rf + 1);

    double part = (double)(fs.second - k) / (double)(fs.second - ds.second); /// remember to cast
    double ans = (double)(fs_val) + part * (double)(ds_val - fs_val);
    ///cout << ds.first << "  " << ds.second << endl;
    ///cout << fs.first << "  " << fs.second << endl;
    cout << (ll)(round(ans)) << endl;
}

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

Compilation message

chorus.cpp: In function 'll cost(ll, ll)':
chorus.cpp:62:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   62 |     if (pre_cost[left] > right)
      |     ^~
chorus.cpp:65:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   65 |         ll lf = pre_cost[left];
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 4 ms 8540 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8536 KB Output is correct
21 Correct 4 ms 8536 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8536 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 4 ms 8540 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8536 KB Output is correct
21 Correct 4 ms 8536 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8536 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
29 Correct 51 ms 8536 KB Output is correct
30 Correct 48 ms 8792 KB Output is correct
31 Correct 48 ms 9048 KB Output is correct
32 Correct 39 ms 8792 KB Output is correct
33 Correct 44 ms 8792 KB Output is correct
34 Correct 48 ms 8792 KB Output is correct
35 Correct 46 ms 8792 KB Output is correct
36 Correct 52 ms 8796 KB Output is correct
37 Correct 52 ms 8796 KB Output is correct
38 Correct 51 ms 8664 KB Output is correct
39 Correct 49 ms 8792 KB Output is correct
40 Correct 43 ms 8536 KB Output is correct
41 Correct 45 ms 8792 KB Output is correct
42 Correct 31 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 4 ms 8540 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8536 KB Output is correct
21 Correct 4 ms 8536 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8536 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
29 Correct 51 ms 8536 KB Output is correct
30 Correct 48 ms 8792 KB Output is correct
31 Correct 48 ms 9048 KB Output is correct
32 Correct 39 ms 8792 KB Output is correct
33 Correct 44 ms 8792 KB Output is correct
34 Correct 48 ms 8792 KB Output is correct
35 Correct 46 ms 8792 KB Output is correct
36 Correct 52 ms 8796 KB Output is correct
37 Correct 52 ms 8796 KB Output is correct
38 Correct 51 ms 8664 KB Output is correct
39 Correct 49 ms 8792 KB Output is correct
40 Correct 43 ms 8536 KB Output is correct
41 Correct 45 ms 8792 KB Output is correct
42 Correct 31 ms 8792 KB Output is correct
43 Correct 868 ms 11852 KB Output is correct
44 Correct 1620 ms 14140 KB Output is correct
45 Correct 1579 ms 14308 KB Output is correct
46 Correct 1217 ms 14136 KB Output is correct
47 Correct 1504 ms 14504 KB Output is correct
48 Correct 1633 ms 14244 KB Output is correct
49 Correct 1559 ms 14248 KB Output is correct
50 Correct 1844 ms 14368 KB Output is correct
51 Correct 1707 ms 14500 KB Output is correct
52 Correct 1571 ms 14240 KB Output is correct
53 Correct 1599 ms 14164 KB Output is correct
54 Correct 1656 ms 14144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8536 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8536 KB Output is correct
10 Correct 1 ms 8540 KB Output is correct
11 Correct 1 ms 8536 KB Output is correct
12 Correct 1 ms 8536 KB Output is correct
13 Correct 1 ms 8536 KB Output is correct
14 Correct 1 ms 8536 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 2 ms 8536 KB Output is correct
17 Correct 2 ms 8536 KB Output is correct
18 Correct 4 ms 8540 KB Output is correct
19 Correct 4 ms 8540 KB Output is correct
20 Correct 3 ms 8536 KB Output is correct
21 Correct 4 ms 8536 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 4 ms 8536 KB Output is correct
24 Correct 4 ms 8536 KB Output is correct
25 Correct 4 ms 8536 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8540 KB Output is correct
28 Correct 4 ms 9048 KB Output is correct
29 Correct 51 ms 8536 KB Output is correct
30 Correct 48 ms 8792 KB Output is correct
31 Correct 48 ms 9048 KB Output is correct
32 Correct 39 ms 8792 KB Output is correct
33 Correct 44 ms 8792 KB Output is correct
34 Correct 48 ms 8792 KB Output is correct
35 Correct 46 ms 8792 KB Output is correct
36 Correct 52 ms 8796 KB Output is correct
37 Correct 52 ms 8796 KB Output is correct
38 Correct 51 ms 8664 KB Output is correct
39 Correct 49 ms 8792 KB Output is correct
40 Correct 43 ms 8536 KB Output is correct
41 Correct 45 ms 8792 KB Output is correct
42 Correct 31 ms 8792 KB Output is correct
43 Correct 868 ms 11852 KB Output is correct
44 Correct 1620 ms 14140 KB Output is correct
45 Correct 1579 ms 14308 KB Output is correct
46 Correct 1217 ms 14136 KB Output is correct
47 Correct 1504 ms 14504 KB Output is correct
48 Correct 1633 ms 14244 KB Output is correct
49 Correct 1559 ms 14248 KB Output is correct
50 Correct 1844 ms 14368 KB Output is correct
51 Correct 1707 ms 14500 KB Output is correct
52 Correct 1571 ms 14240 KB Output is correct
53 Correct 1599 ms 14164 KB Output is correct
54 Correct 1656 ms 14144 KB Output is correct
55 Execution timed out 7013 ms 40504 KB Time limit exceeded
56 Halted 0 ms 0 KB -