#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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2520 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2520 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
6 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
7 ms |
2392 KB |
Output is correct |
24 |
Correct |
7 ms |
2396 KB |
Output is correct |
25 |
Correct |
10 ms |
2652 KB |
Output is correct |
26 |
Correct |
7 ms |
2396 KB |
Output is correct |
27 |
Correct |
7 ms |
2516 KB |
Output is correct |
28 |
Correct |
7 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2520 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
6 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
7 ms |
2392 KB |
Output is correct |
24 |
Correct |
7 ms |
2396 KB |
Output is correct |
25 |
Correct |
10 ms |
2652 KB |
Output is correct |
26 |
Correct |
7 ms |
2396 KB |
Output is correct |
27 |
Correct |
7 ms |
2516 KB |
Output is correct |
28 |
Correct |
7 ms |
2552 KB |
Output is correct |
29 |
Correct |
834 ms |
2900 KB |
Output is correct |
30 |
Correct |
811 ms |
2896 KB |
Output is correct |
31 |
Correct |
793 ms |
2652 KB |
Output is correct |
32 |
Correct |
805 ms |
2652 KB |
Output is correct |
33 |
Correct |
867 ms |
2652 KB |
Output is correct |
34 |
Correct |
808 ms |
2856 KB |
Output is correct |
35 |
Correct |
836 ms |
2864 KB |
Output is correct |
36 |
Correct |
835 ms |
2908 KB |
Output is correct |
37 |
Correct |
840 ms |
2888 KB |
Output is correct |
38 |
Correct |
806 ms |
2892 KB |
Output is correct |
39 |
Correct |
816 ms |
2892 KB |
Output is correct |
40 |
Correct |
933 ms |
2908 KB |
Output is correct |
41 |
Correct |
857 ms |
2884 KB |
Output is correct |
42 |
Correct |
722 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2520 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
6 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
7 ms |
2392 KB |
Output is correct |
24 |
Correct |
7 ms |
2396 KB |
Output is correct |
25 |
Correct |
10 ms |
2652 KB |
Output is correct |
26 |
Correct |
7 ms |
2396 KB |
Output is correct |
27 |
Correct |
7 ms |
2516 KB |
Output is correct |
28 |
Correct |
7 ms |
2552 KB |
Output is correct |
29 |
Correct |
834 ms |
2900 KB |
Output is correct |
30 |
Correct |
811 ms |
2896 KB |
Output is correct |
31 |
Correct |
793 ms |
2652 KB |
Output is correct |
32 |
Correct |
805 ms |
2652 KB |
Output is correct |
33 |
Correct |
867 ms |
2652 KB |
Output is correct |
34 |
Correct |
808 ms |
2856 KB |
Output is correct |
35 |
Correct |
836 ms |
2864 KB |
Output is correct |
36 |
Correct |
835 ms |
2908 KB |
Output is correct |
37 |
Correct |
840 ms |
2888 KB |
Output is correct |
38 |
Correct |
806 ms |
2892 KB |
Output is correct |
39 |
Correct |
816 ms |
2892 KB |
Output is correct |
40 |
Correct |
933 ms |
2908 KB |
Output is correct |
41 |
Correct |
857 ms |
2884 KB |
Output is correct |
42 |
Correct |
722 ms |
2652 KB |
Output is correct |
43 |
Execution timed out |
7053 ms |
7100 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2520 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2520 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
6 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
2396 KB |
Output is correct |
22 |
Correct |
7 ms |
2396 KB |
Output is correct |
23 |
Correct |
7 ms |
2392 KB |
Output is correct |
24 |
Correct |
7 ms |
2396 KB |
Output is correct |
25 |
Correct |
10 ms |
2652 KB |
Output is correct |
26 |
Correct |
7 ms |
2396 KB |
Output is correct |
27 |
Correct |
7 ms |
2516 KB |
Output is correct |
28 |
Correct |
7 ms |
2552 KB |
Output is correct |
29 |
Correct |
834 ms |
2900 KB |
Output is correct |
30 |
Correct |
811 ms |
2896 KB |
Output is correct |
31 |
Correct |
793 ms |
2652 KB |
Output is correct |
32 |
Correct |
805 ms |
2652 KB |
Output is correct |
33 |
Correct |
867 ms |
2652 KB |
Output is correct |
34 |
Correct |
808 ms |
2856 KB |
Output is correct |
35 |
Correct |
836 ms |
2864 KB |
Output is correct |
36 |
Correct |
835 ms |
2908 KB |
Output is correct |
37 |
Correct |
840 ms |
2888 KB |
Output is correct |
38 |
Correct |
806 ms |
2892 KB |
Output is correct |
39 |
Correct |
816 ms |
2892 KB |
Output is correct |
40 |
Correct |
933 ms |
2908 KB |
Output is correct |
41 |
Correct |
857 ms |
2884 KB |
Output is correct |
42 |
Correct |
722 ms |
2652 KB |
Output is correct |
43 |
Execution timed out |
7053 ms |
7100 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |