#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e6;
const long long INF = 1e18;
int a[MAX_N + 1], countLowerA[MAX_N + 1], minGroups[MAX_N + 1];
long long sumA[MAX_N + 1], sumLowerA[MAX_N + 1], minSwaps[MAX_N + 1];
void computeDP( int n, long long cost ) {
for ( int i = 1; i <= n; i++ ) {
minSwaps[i] = INF;
minGroups[i] = n + 1;
}
for ( int i = 1; i <= n; i++ ) {
for ( int j = i - 1; j >= 0; j-- ) {
long long s = minSwaps[j] + (long long)i * (countLowerA[i] - j) - (sumLowerA[i] - sumA[j]) + cost;
if ( s < minSwaps[i] ) {
minSwaps[i] = s;
minGroups[i] = minGroups[j] + 1;
} else if ( s == minSwaps[i] )
minGroups[i] = min( minGroups[i], minGroups[j] + 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 );
sumA[countB] = sumA[countB - 1] + a[countB];
countLowerA[a[countB]]++;
sumLowerA[a[countB]] += a[countB];
}
}
for ( int i = 1; i <= n; i++ ) {
countLowerA[i] += countLowerA[i - 1];
sumLowerA[i] += sumLowerA[i - 1];
}
long long leftCost = 0, rightCost = 1e9;
while ( rightCost - leftCost > 1 ) {
long long cost = (leftCost + rightCost) / 2;
computeDP( n, cost );
if ( minGroups[n] > k )
leftCost = cost;
else
rightCost = cost;
}
computeDP( n, rightCost );
cout << swaps + minSwaps[n] - minGroups[n] * rightCost;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8536 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |