#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int LOG = 20;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
//code
const int maxn = 1e6 + 7;
string s;
int n , k;
int a[maxn];
int posA[maxn] , posB[maxn] , pref[maxn] , timerA , timerB;
namespace sub3
{
int dp[2][5005];
int cost[5005][5005];
void compute()
{
for(int l = 1; l <= n; l++)
{
for(int r = l; r <= n; r++)
{
if(posA[r] > posB[l])
{
cost[l][r] = cost[l][r - 1] + pref[posA[r]] - pref[posB[l] - 1];
}
else cost[l][r] = cost[l][r - 1];
}
}
}
void DnC(int l , int r, int optl , int optr , int cur)
{
if(l > r) return;
int mid = (r + l) >> 1;
int optk;
dp[cur][mid] = inf;
for(int i = optl; i <= min(mid , optr); i++)
{
if(minimize(dp[cur][mid] , dp[!cur][i - 1] + cost[i][mid]))
{
optk = i;
}
}
DnC(l , mid - 1 , optl , optk , cur);
DnC(mid + 1 , r , optk , optr , cur);
}
void solve()
{
ms(dp , 0x3f);
dp[0][0] = 0;
compute();
for(int i = 1; i <= n; i++)
{
dp[1][i] = cost[1][i];
}
for(int i = 2; i <= k; i++)
{
DnC(i , n , 1 , n , i & 1);
}
cout << dp[k & 1][n];
}
}
void solve(void)
{
cin >> n >> k;
cin >> s;
for(int i = 1; i <= 2 * n; i++)
{
pref[i] = pref[i - 1];
if(s[i - 1] == 'A')
{
a[i] = 0;
posA[++timerA] = i;
}
else
{
a[i] = 1;
pref[i] += 1;
posB[++timerB] = i;
}
}
if(n <= 5000) sub3 :: solve();
}
/**
5 2
AABABABBAB
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
}
Compilation message (stderr)
chorus.cpp: In function 'int main()':
chorus.cpp:160:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
160 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
chorus.cpp:161:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |