제출 #1348416

#제출 시각아이디문제언어결과실행 시간메모리
1348416MisterReaperChorus (JOI23_chorus)C++20
61 / 100
7092 ms2176 KiB
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG
    #include "debug.h"
#else
    #define debug(...) void(23)
#endif

template<typename T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

constexpr int max_N = 5000 + 5;
constexpr i64 inf = i64(1E18) + 5;

int N;
int K;
std::string S;

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> N >> K >> S;

    std::vector<int> v[2];
    for (int i = 0; i < 2 * N; ++i) {
        v[S[i] - 'A'].emplace_back(i);
    }

    auto calc = [&](i64 mid) {
        std::vector<std::pair<i64, int>> f(N + 1, {inf, 0});
        f[0] = {0, 0};
        for (int i = 0; i < N; ++i) {
            int cost = 0;
            int p = 0;
            for (int j = 1; i + j <= N; ++j) {
                while (p < N - i && v[1][i + p] < v[0][i + j - 1]) {
                    p += 1;
                }
                cost += p;
                chmin(f[i + j], {f[i].first + cost + mid, f[i].second + 1});
            }
        }
        return f[N];
    };

    i64 lo = 0, hi = 1LL * N * N;
    while (lo < hi) {
        i64 mid = (lo + hi) >> 1;
        if (calc(mid).second <= K) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }

    i64 ans = calc(lo).first - 1LL * lo * K;

    std::cout << ans << '\n';

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