이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#include <cmath>
#define ll long long
#define ld long double
using namespace std;
ll n, k, l, r, mid, dp[1000000];
ll tot[1000000];
vector <ll> A, B, nxA, ps;
string S, T;
ll C(ll l, ll r) {
if (nxA[l] > r) return 0;
ll nx = max(l, nxA[l]), cnt = r-nx+1, cnt2 = max(0LL, l-nxA[l]);
return ps[r]-(nx ? ps[nx-1] : 0)-cnt*B[l]-cnt*(cnt-1)/2-(r-l+1)*cnt2;
}
void solve() {
dp[0] = C(0, 0) + mid, tot[0] = 1;
for (int i=1; i<n; ++i) {
dp[i] = C(0, i) + mid, tot[i] = 1;
for (int j=0; j<i; ++j) {
if (dp[i] > dp[j] + C(j+1, i) + mid || ((dp[i] == dp[j] + C(j+1, i) + mid) && tot[j]+1 > tot[i])) {
dp[i] = dp[j] + C(j+1, i) + mid;
tot[i] = tot[j] + 1;
}
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cin >> n >> k >> S;
for (int i=0; i<2*n; ++i) {
if (S[i] == 'A') {
ps.push_back((ps.empty() ? 0 : ps.back()) + i);
A.push_back(i);
}
else {
B.push_back(i);
nxA.push_back(A.size());
}
}
l = 0, r = n*n;
while (l < r) {
mid = (l+r+1)/2;
solve();
if (k > tot[n-1]) r = mid-1;
else l = mid;
}
mid = l;
solve();
cout << dp[n-1] - k * mid << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |