Submission #767405

#TimeUsernameProblemLanguageResultExecution timeMemory
767405PurpleCrayonChorus (JOI23_chorus)C++17
40 / 100
7055 ms125004 KiB
#include <bits/stdc++.h>
using namespace std;

#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e3+10, MOD = 1e9+7;

int n, K, small[N];
vector<int> one, two;
int dp[N][N], cost[N][N];

void solve() {
    cin >> n >> K;
    for (int i = 0; i < 2 * n; i++) {
        char c; cin >> c;
        if (c == 'A') one.push_back(i);
        else two.push_back(i);
    }

    // for (int x : one) cerr << x << ' '; cerr << endl;
    // for (int x : two) cerr << x << ' '; cerr << endl;

    for (int i = 0; i < n; i++) {
        small[i] = lower_bound(two.begin(), two.end(), one[i]) - two.begin();
    }

    for (int l = 0; l < n; l++) {
        int cur = 0;
        for (int r = l; r < n; r++) {
            cur += max(small[r] - l, 0);
            cost[l][r] = cur;
        }
    }

    for (int i = 0; i < n; i++) for (int j = 0; j <= K; j++) dp[i][j] = MOD;

    for (int i = 0; i < n; i++) {
        for (int l = i; l >= 0; l--) {
            for (int k = 1; k <= K; k++) {
                int prv = MOD;
                if (l == 0) prv = (k == 1 ? 0 : MOD);
                else prv = dp[l-1][k-1];

                dp[i][k] = min(dp[i][k], cost[l][i] + prv);
            }
        }
    }

    cout << dp[n-1][K] << '\n';
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

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