#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e3;
int a[MAX_N + 1], sa[MAX_N + 1], minSwaps[MAX_N + 1][MAX_N + 1];
int main() {
int n, k, swaps;
cin >> n >> k;
int countB = 0, countA = 0;
swaps = 0;
for ( int i = 0; i < 2 * n; i++ ) {
char ch;
cin >> ch;
if ( ch == 'A' )
countA++;
else {
countB++;
swaps += max( 0, countB - countA );
a[countB] = max( countA, countB );
sa[countB] = sa[countB - 1] + a[countB];
}
}
for ( int i = 1; i <= n; i++ )
minSwaps[i][0] = n * n;
for ( int l = 1; l <= k; l++ ) {
for ( int i = 1; i <= n; i++ ) {
minSwaps[i][l] = n * n;
int p = i;
for ( int j = i - 1; j >= 0 && a[j + 1] >= i; j-- ) {
minSwaps[i][l] = min( minSwaps[i][l], minSwaps[j][l - 1] );
p = j;
}
for ( int j = p - 1; j >= 0; j-- )
minSwaps[i][l] = min( minSwaps[i][l], minSwaps[j][l - 1] - i * j + sa[j] + i * p - sa[p] );
}
}
cout << swaps + minSwaps[n][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
4444 KB |
Output is correct |
18 |
Correct |
16 ms |
10728 KB |
Output is correct |
19 |
Correct |
41 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
73 ms |
10748 KB |
Output is correct |
23 |
Correct |
83 ms |
10984 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
75 ms |
10736 KB |
Output is correct |
26 |
Correct |
62 ms |
10744 KB |
Output is correct |
27 |
Correct |
27 ms |
10588 KB |
Output is correct |
28 |
Correct |
26 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
4444 KB |
Output is correct |
18 |
Correct |
16 ms |
10728 KB |
Output is correct |
19 |
Correct |
41 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
73 ms |
10748 KB |
Output is correct |
23 |
Correct |
83 ms |
10984 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
75 ms |
10736 KB |
Output is correct |
26 |
Correct |
62 ms |
10744 KB |
Output is correct |
27 |
Correct |
27 ms |
10588 KB |
Output is correct |
28 |
Correct |
26 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7031 ms |
96960 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
4444 KB |
Output is correct |
18 |
Correct |
16 ms |
10728 KB |
Output is correct |
19 |
Correct |
41 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
73 ms |
10748 KB |
Output is correct |
23 |
Correct |
83 ms |
10984 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
75 ms |
10736 KB |
Output is correct |
26 |
Correct |
62 ms |
10744 KB |
Output is correct |
27 |
Correct |
27 ms |
10588 KB |
Output is correct |
28 |
Correct |
26 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7031 ms |
96960 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
4444 KB |
Output is correct |
18 |
Correct |
16 ms |
10728 KB |
Output is correct |
19 |
Correct |
41 ms |
10588 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
73 ms |
10748 KB |
Output is correct |
23 |
Correct |
83 ms |
10984 KB |
Output is correct |
24 |
Correct |
4 ms |
10588 KB |
Output is correct |
25 |
Correct |
75 ms |
10736 KB |
Output is correct |
26 |
Correct |
62 ms |
10744 KB |
Output is correct |
27 |
Correct |
27 ms |
10588 KB |
Output is correct |
28 |
Correct |
26 ms |
10588 KB |
Output is correct |
29 |
Execution timed out |
7031 ms |
96960 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |