This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e3;
int a[MAX_N + 1];
long long sumA[MAX_N + 1], countLowerA[MAX_N + 1], sumLowerA[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 );
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];
}
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;
for ( int j = i - 1; j >= 0; j-- )
minSwaps[i][l] = min( minSwaps[i][l], minSwaps[j][l - 1] + i * (countLowerA[i] - j) - (sumLowerA[i] - sumA[j]) );
}
}
cout << swaps + minSwaps[n][k];
return 0;
}
# | 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... |