#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define ld double
#define ll int64_t
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
#define maxn 100005
ll n,k;
ll a[2*maxn];
ll c[maxn];
ll ps[maxn];
ll sz = 0;
ll dp[maxn];
struct line{
ll n,k,cnti;
line(){n = llinf;k = 0;cnti = 0;}
line(ll n_,ll k_,ll cnti_){n = n_,k = k_,cnti = cnti_;}
ll operator()(ll x){
return k*x+n;
}
};
line t[2*maxn];
ll ls[2*maxn],rs[2*maxn],tsz = 0,root = 0;
void init(ll &v,ll tl,ll tr){
if(!v) v = ++tsz;
t[v] = line();
if(tl==tr) return;
ll mid = (tl+tr)/2;
init(ls[v],tl,mid);
init(rs[v],mid+1,tr);
}
bool cmp(line f,line g,ll i){
if(f(i)==g(i)){
return f.cnti>g.cnti;
}
return f(i)<g(i);
}
line mini(line f,line g,ll i){
if(cmp(f,g,i)) return f;
return g;
}
void upd(ll v,ll tl,ll tr,line f){
line g = t[v];
if(tl==tr){
if(cmp(f,g,tl)) t[v] = f;
return;
}
ll mid = (tl+tr)/2;
if(cmp(g,f,tl)) swap(f,g);
if(cmp(f,g,mid)){t[v] = f;upd(rs[v],mid+1,tr,g);}
else{t[v] = g;upd(ls[v],tl,mid,f);}
}
line get(ll v,ll tl,ll tr,ll i){
if(tl==tr) return t[v];
ll mid = (tl+tr)/2;
if(i<=mid) return mini(get(ls[v],tl,mid,i),t[v],i);
return mini(get(rs[v],mid+1,tr,i),t[v],i);
}
ll cnt[maxn];
ll check(ll x){
multiset<pll> s;
init(root,1,n);
ll e = 1;
s.insert({0,0});
dp[0] = 0;
cnt[0] = 0;
for(ll i = 1;i<=n;i++){
while(e<=i&&c[e]<i){
s.erase(s.find({dp[e-1],-cnt[e-1]}));
line f(ps[e-1]+dp[e-1],-e,cnt[e-1]);
upd(root,1,n,f);
e++;
}
dp[i] = llinf*2; cnt[i] = 0;
if(si(s)){
dp[i] = (*s.begin()).fi + x;
cnt[i] = -(*s.begin()).sc + 1;
}
line f = get(root,1,n,i);
if(f(i) - ps[e-1] + e*i + x < dp[i]){
dp[i] = f(i) - ps[e-1] + e*i + x;
cnt[i] = f.cnti+1;
}else if(f(i) - ps[e-1] + e*i + x == dp[i]&&f.cnti+1>cnt[i]){
cnt[i] = f.cnti+1;
dbg(i);
}
s.insert({dp[i],-cnt[i]});
}
return cnt[n];
}
ll bs(){
ll l = 0,r = llinf/10,mid,rez = 0;
while(l<=r){
mid = (l+r)/2;
ll e = check(mid);
if(e<k){
r = mid-1;
}else{
rez = mid;
l = mid+1;
}
}
check(rez);
return dp[n]-k*rez;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> k;
string s; cin >> s;
for(ll i = 1;i<=2*n;i++){
if(s[i-1]=='A') a[i] = 1;
}
ll b = 0;
for(ll i = 2*n;i>=1;i--){
if(a[i]==1){
c[++sz] = b;
}else b++;
}
for(ll i = 1;i<=n;i++) ps[i] = ps[i-1] + c[i];
//for(ll i = 0;i<=n;i++) ceri(dp[i],0,k);
cout<<bs()<<endl;
return (0-0);
}
/**
5 2
AABABABBAB
10 9
BBBBBBBBBBAAAAAAAAAA
10 4
ABAAABBBAAABBBAABBAB
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
12 ms |
5204 KB |
Output is correct |
19 |
Correct |
9 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5076 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
5092 KB |
Output is correct |
23 |
Correct |
7 ms |
5076 KB |
Output is correct |
24 |
Correct |
8 ms |
5104 KB |
Output is correct |
25 |
Correct |
7 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5076 KB |
Output is correct |
27 |
Correct |
8 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
12 ms |
5204 KB |
Output is correct |
19 |
Correct |
9 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5076 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
5092 KB |
Output is correct |
23 |
Correct |
7 ms |
5076 KB |
Output is correct |
24 |
Correct |
8 ms |
5104 KB |
Output is correct |
25 |
Correct |
7 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5076 KB |
Output is correct |
27 |
Correct |
8 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5076 KB |
Output is correct |
29 |
Correct |
68 ms |
5460 KB |
Output is correct |
30 |
Correct |
89 ms |
5476 KB |
Output is correct |
31 |
Correct |
94 ms |
5472 KB |
Output is correct |
32 |
Correct |
67 ms |
5432 KB |
Output is correct |
33 |
Correct |
67 ms |
5428 KB |
Output is correct |
34 |
Correct |
75 ms |
5428 KB |
Output is correct |
35 |
Correct |
74 ms |
5432 KB |
Output is correct |
36 |
Correct |
90 ms |
5468 KB |
Output is correct |
37 |
Correct |
84 ms |
5468 KB |
Output is correct |
38 |
Correct |
68 ms |
5472 KB |
Output is correct |
39 |
Correct |
74 ms |
5472 KB |
Output is correct |
40 |
Correct |
76 ms |
5472 KB |
Output is correct |
41 |
Correct |
76 ms |
5472 KB |
Output is correct |
42 |
Correct |
76 ms |
5588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
12 ms |
5204 KB |
Output is correct |
19 |
Correct |
9 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5076 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
5092 KB |
Output is correct |
23 |
Correct |
7 ms |
5076 KB |
Output is correct |
24 |
Correct |
8 ms |
5104 KB |
Output is correct |
25 |
Correct |
7 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5076 KB |
Output is correct |
27 |
Correct |
8 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5076 KB |
Output is correct |
29 |
Correct |
68 ms |
5460 KB |
Output is correct |
30 |
Correct |
89 ms |
5476 KB |
Output is correct |
31 |
Correct |
94 ms |
5472 KB |
Output is correct |
32 |
Correct |
67 ms |
5432 KB |
Output is correct |
33 |
Correct |
67 ms |
5428 KB |
Output is correct |
34 |
Correct |
75 ms |
5428 KB |
Output is correct |
35 |
Correct |
74 ms |
5432 KB |
Output is correct |
36 |
Correct |
90 ms |
5468 KB |
Output is correct |
37 |
Correct |
84 ms |
5468 KB |
Output is correct |
38 |
Correct |
68 ms |
5472 KB |
Output is correct |
39 |
Correct |
74 ms |
5472 KB |
Output is correct |
40 |
Correct |
76 ms |
5472 KB |
Output is correct |
41 |
Correct |
76 ms |
5472 KB |
Output is correct |
42 |
Correct |
76 ms |
5588 KB |
Output is correct |
43 |
Correct |
1176 ms |
10084 KB |
Output is correct |
44 |
Correct |
2053 ms |
13268 KB |
Output is correct |
45 |
Correct |
2056 ms |
13344 KB |
Output is correct |
46 |
Correct |
1646 ms |
12504 KB |
Output is correct |
47 |
Correct |
1623 ms |
12536 KB |
Output is correct |
48 |
Correct |
1543 ms |
12504 KB |
Output is correct |
49 |
Correct |
1550 ms |
12580 KB |
Output is correct |
50 |
Correct |
2005 ms |
13284 KB |
Output is correct |
51 |
Correct |
2058 ms |
13348 KB |
Output is correct |
52 |
Correct |
1633 ms |
13344 KB |
Output is correct |
53 |
Correct |
1606 ms |
13272 KB |
Output is correct |
54 |
Correct |
1868 ms |
13272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5016 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
12 ms |
5204 KB |
Output is correct |
19 |
Correct |
9 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5076 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
5092 KB |
Output is correct |
23 |
Correct |
7 ms |
5076 KB |
Output is correct |
24 |
Correct |
8 ms |
5104 KB |
Output is correct |
25 |
Correct |
7 ms |
5076 KB |
Output is correct |
26 |
Correct |
7 ms |
5076 KB |
Output is correct |
27 |
Correct |
8 ms |
5100 KB |
Output is correct |
28 |
Correct |
8 ms |
5076 KB |
Output is correct |
29 |
Correct |
68 ms |
5460 KB |
Output is correct |
30 |
Correct |
89 ms |
5476 KB |
Output is correct |
31 |
Correct |
94 ms |
5472 KB |
Output is correct |
32 |
Correct |
67 ms |
5432 KB |
Output is correct |
33 |
Correct |
67 ms |
5428 KB |
Output is correct |
34 |
Correct |
75 ms |
5428 KB |
Output is correct |
35 |
Correct |
74 ms |
5432 KB |
Output is correct |
36 |
Correct |
90 ms |
5468 KB |
Output is correct |
37 |
Correct |
84 ms |
5468 KB |
Output is correct |
38 |
Correct |
68 ms |
5472 KB |
Output is correct |
39 |
Correct |
74 ms |
5472 KB |
Output is correct |
40 |
Correct |
76 ms |
5472 KB |
Output is correct |
41 |
Correct |
76 ms |
5472 KB |
Output is correct |
42 |
Correct |
76 ms |
5588 KB |
Output is correct |
43 |
Correct |
1176 ms |
10084 KB |
Output is correct |
44 |
Correct |
2053 ms |
13268 KB |
Output is correct |
45 |
Correct |
2056 ms |
13344 KB |
Output is correct |
46 |
Correct |
1646 ms |
12504 KB |
Output is correct |
47 |
Correct |
1623 ms |
12536 KB |
Output is correct |
48 |
Correct |
1543 ms |
12504 KB |
Output is correct |
49 |
Correct |
1550 ms |
12580 KB |
Output is correct |
50 |
Correct |
2005 ms |
13284 KB |
Output is correct |
51 |
Correct |
2058 ms |
13348 KB |
Output is correct |
52 |
Correct |
1633 ms |
13344 KB |
Output is correct |
53 |
Correct |
1606 ms |
13272 KB |
Output is correct |
54 |
Correct |
1868 ms |
13272 KB |
Output is correct |
55 |
Runtime error |
13 ms |
17208 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |