This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 5100;
int n, k;
string s;
int dp[maxn][maxn], pos[2][maxn];
/**
*/
void chmin(int &var, int val) /// careful for long long
{
var = min(var, val);
}
int cost[maxn][maxn];
void precalc()
{
for (int j = 1; j <= n; j ++)
{
int tk = 0;
for (int fix = 1; fix <= n - j; fix ++)
{
if (pos[0][fix + j] > j * 2 + fix)
tk += pos[0][fix + j] - (j * 2 + fix);
cost[j][fix] = tk;
}
}
}
void solve()
{
cin >> n >> k >> s;
s = "#" + s;
int cntA = 0, cntB = 0;
for (int i = 1; i <= 2 * n; i ++)
{
if (s[i] == 'A')
{
cntA ++;
pos[0][cntA] = i;
}
else
{
cntB ++;
pos[1][cntB] = i;
}
}
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= n; j ++)
dp[i][j] = 4 * n * n; /// careful for overflow
precalc();
int cr = 0;
for (int j = 1; j <= n; j ++)
{
cr = cr + pos[0][j] - j;
dp[1][j] = cr;
}
for (int i = 1; i < k; i ++)
{
for (int j = 1; j <= n; j ++)
{
if (dp[i][j] == 4 * n * n)
continue;
for (int fix = 1; fix <= n - j; fix ++)
{
chmin(dp[i + 1][j + fix], dp[i][j] + cost[j][fix]);
}
}
}
int ans = 4 * n * n;
for (int i = 1; i <= k; i ++)
ans = min(ans, dp[i][n]);
cout << ans << endl;
}
int main()
{
///freopen("test.txt", "r", stdin);
solve();
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... |