/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
const double eps = 0.0000001;
struct St{
double cost = 0;
int sz = 0;
int cur_sz = 0;
};
int n, k;
ll A[N], B[N];
string s;
vector<ll> pref(2*N+1), pref2(2*N+1);
pair<double, int> f(double cost){
vector<array<St, 2>> DP(n+1);
for(int i = 1; i <= n; ++i){
int x = i, y = i - 1;
if(DP[y][0].cost < DP[y][1].cost){
DP[x][0].cost = DP[y][0].cost + cost + (B[i] < A[i] ? pref[A[i]] - pref[B[i]] : 0ll);
DP[x][0].sz = DP[y][0].sz + 1;
DP[x][0].cur_sz = 1;
DP[x][1].cost = DP[y][0].cost + (B[i] < A[i] ? pref[A[i]] - pref[B[i]] : 0ll) + (B[i] < A[i] ? DP[y][0].cur_sz : max(0ll, pref2[A[i]] - (i - DP[y][0].cur_sz)));
DP[x][1].sz = DP[y][0].sz;
DP[x][1].cur_sz = DP[y][0].cur_sz + 1;
}else{
DP[x][0].cost = DP[y][1].cost + cost + (B[i] < A[i] ? pref[A[i]] - pref[B[i]] : 0ll);
DP[x][0].sz = DP[y][1].sz + 1;
DP[x][0].cur_sz = 1;
DP[x][1].cost = DP[y][1].cost + (B[i] < A[i] ? pref[A[i]] - pref[B[i]] : 0ll) + (B[i] < A[i] ? DP[y][1].cur_sz : max(0ll, pref2[A[i]] - (i - DP[y][0].cur_sz)));
DP[x][1].sz = DP[y][1].sz;
DP[x][1].cur_sz = DP[y][1].cur_sz + 1;
}
if(i == 1) DP[x][1] = DP[x][0];
// cout << DP[x][0].cost << ' ' << DP[x][0].sz << " | ";
// cout << DP[x][1].cost << ' ' << DP[x][1].sz << "\n";
}
// en;en;
return (DP[n][0].cost < DP[n][1].cost ? pair<double, int>{DP[n][0].cost, DP[n][0].sz} : pair<double, int>{DP[n][1].cost, DP[n][1].sz});
}
void solve(){
cin >> n >> k >> s;
assert(n != 1);
assert(k != 1);
assert(s != "AB");
int a = 0, b = 0;
A[0] = 0;
for(int i = 0; i < 2*n; ++i){
pref[i + 1] = pref[i];
pref2[i + 1] = pref2[i];
if(s[i] == 'A'){
A[++a] = i+1;
pref[i + 1]++;
}else{
B[++b] = i+1;
pref2[i + 1]++;
}
}
ll ans = 0;
double l = 0, r = MOD;
while(l + eps < r){
double m = (l+r)/2;
// cout << m << ":\n";
auto p = f(m);
// cout << p.ff << ' ' << p.ss << '\n';
if(p.ss >= k){
ans = (ll)(p.ff - p.ss * m);
l = m;
}else{
r = m;
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
cout << fixed << setprecision(10);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
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... |