#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], 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;
int j = 0;
for (int i=1; i<n; ++i) {
dp[i] = C(0, i) + mid, tot[i] = 1;
while (j+1 < i) {
if (dp[j] + C(j+1, i) > dp[j+1] + C(j+2, i)) ++j;
else break;
}
if (dp[i] > dp[j] + C(j+1, i) + mid) {
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)/2;
solve();
if (tot[n-1] > k) l = mid+1;
else r = mid;
}
mid = l;
solve();
cout << dp[n-1] - k * mid;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |