이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#define ll long long
using namespace std;
ll n, k, l, r, mid, dp[1000000];
array <ll, 2> 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, 1};
int j = 0, x = 0;
for (int i=1; i<n; ++i) {
dp[i] = C(0, i) + mid, tot[i] = {1, 1};
while (j+1 < i) {
if ((j == -1 ? C(j+1, i) : dp[j] + C(j+1, i)) > dp[j+1] + C(j+2, i)) ++j;
else break;
}
while (x+1 < i) {
if ((x == -1 ? C(x+1, i) : dp[x] + C(x+1, i)) > dp[x+1] + C(x+2, i)) ++x, j = x;
else if ((x == -1 ? C(x+1, i) : dp[x] + C(x+1, i)) == dp[x+1] + C(x+2, i)) ++x;
else break;
}
if (dp[i] > (j == -1 ? C(j+1, i) : dp[j] + C(j+1, i)) + mid) {
dp[i] = (j == -1 ? C(j+1, i) : dp[j] + C(j+1, i)) + mid;
tot[i][0] = (j == -1 ? 0 : tot[j][0]) + 1;
tot[i][1] = (x == -1 ? 0 : tot[x][1]) + 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][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... |