Submission #796381

#TimeUsernameProblemLanguageResultExecution timeMemory
796381JohannChorus (JOI23_chorus)C++14
40 / 100
7087 ms79436 KiB
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const ll INF = 1LL << 60;
int N, K;
vi S;
int cnt[2];
vi pos[2];
vvi dp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> K;
    string SS;
    cin >> SS;
    S.resize(2 * N);
    for (int foo = 0; foo < 2; ++foo)
        pos[foo].assign(N, -1);
    for (int i = 0; i < 2 * N; ++i)
    {
        S[i] = (SS[i] == 'B');
        pos[S[i]][cnt[S[i]]++] = i;
    }

    dp = vvi(N + 1, vi(K + 1, INF));

    dp[0][0] = 0;
    dp[0][1] = 0;
    for (int i = 0; i < N; ++i)
        dp[i + 1][1] = dp[i][1] + pos[0][i] - i;

    for (int k = 1; k < K; ++k)
    {
        // compute k + 1 with k and iterative approach
        for (int i = k; i <= N; ++i)
        {
            // working from base i times 0 and i times 1;
            ll tmp = dp[i][k];
            int border = 2 * i;
            for (int j = i; j < N; ++j)
            {
                if (pos[0][j] < border)
                    ++border;
                else
                    tmp += pos[0][j] - border++;
                dp[j + 1][k + 1] = min(dp[j + 1][k + 1], tmp);
            }
        }
    }

    cout << dp[N][K] << endl;

    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...