Submission #746274

#TimeUsernameProblemLanguageResultExecution timeMemory
746274baluteshihChorus (JOI23_chorus)C++14
40 / 100
128 ms2312 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

int lft[1000005], lst[1000005];
ll prv[1000005];

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    if (n > 500) return 0;

    int cntA = 0, cntB = 0;
    for (int i = 0; i < n + n; ++i) {
        if (s[i] == 'B') ++cntB;
        else lft[++cntA] = cntB;
    }
    for (int i = 1; i <= n; ++i)
        prv[i] = prv[i - 1] + lft[i];
    for (int i = 0; i <= n; ++i)
        lst[lft[i]] = i;
    for (int i = 1; i <= n; ++i)
        lst[i] = max(lst[i], lst[i - 1]);
    for (int i = 0; i <= n; ++i)
        lst[i] = max(i, lst[i]);

    const ll INF = 1e18;
    vector<vector<ll>> dp(k + 1, vector<ll>(n + 1, INF));
    ll fin = INF;
    dp[0][0] = 0;
    for (int i = 1; i <= k; ++i) { 
        for (int j = 1, p = 0; j <= n; ++j) {
            while (lst[p] <= j && p < j) ++p;
            if (p < j) 
                dp[i][j] = min(dp[i][j], dp[i - 1][p]);
            for (int q = 0; q < p; ++q)
                dp[i][j] = min(dp[i][j], dp[i - 1][q] + prv[j] - prv[lst[q]] - q * (j - lst[q]));
        }
        fin = min(fin, dp[i][n]);
    }

    cout << fin << "\n";
}
#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...