#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define pp pop_back
#define mp make_pair
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vii = vector<pii>;
using ld = long double;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
cerr<<h;
if(sizeof...(t)){
cerr<<", ";
}
debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<17;
ld dp[N], ile[N];
int main(){
int n, k;
string s;
cin>>n>>k>>s;
ll res=0;
vi V(n+1), pocz(n+1);
vector<ll> sum={0};
int a=0, b=1;
for(char c:s){
if(c=='A'){
a++;
pocz[a]=min(b, a)-1;
}
else{
res+=max(0, b-a);
V[b]=max(a, b);
sum.pb(V[b]+sum.back());
b++;
}
}
deb(res);
for(int i=1; i<=n; i++){
//deb(i, V[i], pocz[i], sum[i]);
}
/*vector<vector<ll> > dp2(n+1, vector<ll>(k+1, 1e18));
dp2[0][0]=0;
for(int kk=1; kk<=k; kk++){
for(int i=kk; i<=n; i++){
ll c=0;
for(int j=i-1; j>=0; j--){
c+=max(0, i-V[j+1]);
dp2[i][kk]=min(dp2[i][kk], dp2[j][kk-1]+c);
}
}
}
for(int kk=1; kk<=k; kk++){
cout<<dp2[n][kk]<<" ";
}*/
ld l=-1e12, r=1e12;
pair<int, ll> L,R;
for(int ii=0; ii<70; ii++){
ld m=(l+r)/2;
dp[0]=0;
ile[0]=0;
deque<pair<ld, int> > V2;
deque<int> tim;
V2.eb(dp[0], 0);
deb(m);
for(int i=1; i<=n; i++){
for(int j=pocz[i-1]+1; j<=pocz[i]; j++){
ld A=dp[j]+sum[j];
int B=-j;
//deb(A, B);
while(V2.size() && A<=V2.back().st){
V2.pp();
if(tim.size())tim.pp();
}
if(V2.empty()){
V2.eb(A, B);
continue;
}
ll t=ceil((A-V2.back().st)/(V2.back().nd-B))+1e-9;
//deb(t);
while(V2.size()>=2){
if(t<=tim.back()){
V2.pp();
tim.pp();
t=ceil((A-V2.back().st)/(V2.back().nd-B))+1e-9;
}
else break;
}
//if(t<=n){
V2.eb(A,B);
tim.pb(min(t, ll(n+1)));
//}
}
while(tim.size() && tim.front()<=i){
tim.pop_front();
V2.pop_front();
}
dp[i]=V2.front().st+i*1ll*V2.front().nd+pocz[i]*1ll*i-sum[pocz[i]];
ile[i]=ile[-V2.front().nd]+1;
if(dp[i]>dp[i-1]){
dp[i]=dp[i-1];
ile[i]=ile[i-1]+1;
}
if(dp[i]>dp[pocz[i]]){
dp[i]=dp[pocz[i]];
ile[i]=ile[pocz[i]]+1;
}
dp[i]+=m;
//deb(i, dp[i]);
}
/*int opt=0;
//deb(m);
for(int i=1; i<=n; i++){
ll c=0;
//dp[i]=1e18;
//deb(i);
if(i>1 && opt==0)opt++;
ll c2=0;
if(pocz[i]>opt)c2=(pocz[i]-opt)*1ll*i-sum[pocz[i]]+sum[opt];
//dp[opt]+sum[opt]-opt*i dla opt<=pocz[i]
//czyli jesli opt<opt2 i f(opt)>f(opt2) to zawsze pozniej bedzie
//wiec i bedzie lepsze od i-1 od pewnego momentu
//mozemy wrzucic wszystko na vector
/*while(opt<i-1){
ll c3=0;
if(pocz[i]>opt+1)c3=(pocz[i]-opt-1)*1ll*i-sum[pocz[i]]+sum[opt+1];
if(dp[opt]+c2>=dp[opt+1]+c3){
opt++;
c2=c3;
}
else break;
}*/
/*
//if(dp[i]>dp[opt]+c2+m){
dp[i]=dp[opt]+c2+m;
ile[i]=ile[opt]+1;
//}
c2=0;
if(pocz[i]>0)c2=pocz[i]*1ll*i-sum[pocz[i]];
if(dp[i]>dp[0]+c2+m){
dp[i]=dp[0]+c2+m;
ile[i]=ile[0]+1;
}
deb(i, opt, dp[i], pocz[i]);
for(int j=i-1; j>=0; j--){
c+=max(0, i-V[j+1]);
c2=0;
if(pocz[i]>j)c2=(pocz[i]-j)*1ll*i-sum[pocz[i]]+sum[j];
deb(i, j, dp[i], dp[j]+c+m);
assert(c==c2);
//deb(j, dp[j], dp[j]+c+m);
}
c=0;
ld x=1e18;
for(int j=i-1; j>=0; j--){
c+=max(0, i-V[j+1]);
c2=0;
if(pocz[i]>j)c2=(pocz[i]-j)*1ll*i-sum[pocz[i]]+sum[j];
//deb(i, j, dp[i], dp[j]+c+m);
assert(c==c2);
//deb(j, dp[j], dp[j]+c+m);
if(x>dp[j]+c+m){
x=dp[j]+c+m;
//ile[i]=ile[j]+1;
}
}
assert(x==dp[i]);
}*/
if(ile[n]>k){
l=m;
R=mp(ile[n], ll(dp[n]-k*m+0.5));
}
else if(ile[n]<k){
r=m;
L=mp(ile[n], ll(dp[n]-k*m+0.5));
}
else{
cout<<res+ll(dp[n]-k*m+0.5);
return 0;
}
}
assert((R.nd*(k-L.st)+L.nd*(R.st-k))%(R.st-L.st)==0);
ll ans=(R.nd*(k-L.st)+L.nd*(R.st-k))/(R.st-L.st);
cout<<ans+res;
//assert(false);
}
Compilation message
chorus.cpp:135:4: warning: "/*" within comment [-Wcomment]
135 | /*while(opt<i-1){
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
17 ms |
664 KB |
Output is correct |
30 |
Correct |
18 ms |
596 KB |
Output is correct |
31 |
Correct |
19 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
24 ms |
652 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
596 KB |
Output is correct |
37 |
Correct |
30 ms |
596 KB |
Output is correct |
38 |
Correct |
15 ms |
648 KB |
Output is correct |
39 |
Correct |
19 ms |
664 KB |
Output is correct |
40 |
Correct |
10 ms |
596 KB |
Output is correct |
41 |
Correct |
15 ms |
660 KB |
Output is correct |
42 |
Correct |
9 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
17 ms |
664 KB |
Output is correct |
30 |
Correct |
18 ms |
596 KB |
Output is correct |
31 |
Correct |
19 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
24 ms |
652 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
596 KB |
Output is correct |
37 |
Correct |
30 ms |
596 KB |
Output is correct |
38 |
Correct |
15 ms |
648 KB |
Output is correct |
39 |
Correct |
19 ms |
664 KB |
Output is correct |
40 |
Correct |
10 ms |
596 KB |
Output is correct |
41 |
Correct |
15 ms |
660 KB |
Output is correct |
42 |
Correct |
9 ms |
596 KB |
Output is correct |
43 |
Correct |
227 ms |
3436 KB |
Output is correct |
44 |
Correct |
427 ms |
7084 KB |
Output is correct |
45 |
Correct |
418 ms |
5336 KB |
Output is correct |
46 |
Correct |
13 ms |
7000 KB |
Output is correct |
47 |
Correct |
47 ms |
7068 KB |
Output is correct |
48 |
Correct |
294 ms |
7080 KB |
Output is correct |
49 |
Correct |
10 ms |
5260 KB |
Output is correct |
50 |
Correct |
107 ms |
7076 KB |
Output is correct |
51 |
Correct |
252 ms |
7080 KB |
Output is correct |
52 |
Correct |
9 ms |
5336 KB |
Output is correct |
53 |
Correct |
288 ms |
7080 KB |
Output is correct |
54 |
Correct |
483 ms |
7076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
17 ms |
664 KB |
Output is correct |
30 |
Correct |
18 ms |
596 KB |
Output is correct |
31 |
Correct |
19 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
4 ms |
596 KB |
Output is correct |
34 |
Correct |
24 ms |
652 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
596 KB |
Output is correct |
37 |
Correct |
30 ms |
596 KB |
Output is correct |
38 |
Correct |
15 ms |
648 KB |
Output is correct |
39 |
Correct |
19 ms |
664 KB |
Output is correct |
40 |
Correct |
10 ms |
596 KB |
Output is correct |
41 |
Correct |
15 ms |
660 KB |
Output is correct |
42 |
Correct |
9 ms |
596 KB |
Output is correct |
43 |
Correct |
227 ms |
3436 KB |
Output is correct |
44 |
Correct |
427 ms |
7084 KB |
Output is correct |
45 |
Correct |
418 ms |
5336 KB |
Output is correct |
46 |
Correct |
13 ms |
7000 KB |
Output is correct |
47 |
Correct |
47 ms |
7068 KB |
Output is correct |
48 |
Correct |
294 ms |
7080 KB |
Output is correct |
49 |
Correct |
10 ms |
5260 KB |
Output is correct |
50 |
Correct |
107 ms |
7076 KB |
Output is correct |
51 |
Correct |
252 ms |
7080 KB |
Output is correct |
52 |
Correct |
9 ms |
5336 KB |
Output is correct |
53 |
Correct |
288 ms |
7080 KB |
Output is correct |
54 |
Correct |
483 ms |
7076 KB |
Output is correct |
55 |
Runtime error |
57 ms |
33840 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |