Submission #906219

#TimeUsernameProblemLanguageResultExecution timeMemory
906219LucaIlieChorus (JOI23_chorus)C++17
40 / 100
7062 ms96932 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 5e3;
int a[MAX_N + 1], dp[MAX_N + 1][MAX_N + 1];

int main() {
    int n, k;

    cin >> n >> k;

    int countB = 0, countA = 0;
    for ( int i = 0; i < 2 * n; i++ ) {
        char ch;
        cin >> ch;
        if ( ch == 'A' )
            countA++;
        else {
            countB++;
            a[countB] = countA;
        }
    }

    for ( int i = 1; i <= n; i++ )
        dp[i][0] = n * n;
    for ( int l = 1; l <= k; l++ ) {
        for ( int i = 1; i <= n; i++ ) {
            dp[i][l] = n * n;
            int swapsAdded = 0, j;
            for ( j = i - 1; j >= 0 && a[j + 1] >= i; j-- )
                dp[i][l] = min( dp[i][l], dp[j][l - 1] );
            for ( ; j >= 0; j-- ) {
                swapsAdded += i - a[j + 1];
                dp[i][l] = min( dp[i][l], dp[j][l - 1] + swapsAdded );
            }
        }
    }

    cout << dp[n][k];

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