#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;
#define f first
#define s second
#define eb emplace_back
const ll inf=1e18;
int a[1000005],b[1000005],pos[1000005],pos2[1000005];
ll dp2[2][1000005],qs[1000005];
ll f(int L,int R){
int x=pos2[L];
if(x>R) return 0;
return qs[R]-qs[x-1]-L*(R-x+1);
}
void play2(int n,bool z){
for(int i=1;i<=n;++i){
dp2[z][i]=inf;
for(int j=0;j<i;++j){
if(pos2[j+1]>i){
dp2[z][i]=min(dp2[z][i],dp2[!z][j]);
break;
}
dp2[z][i]=min(dp2[z][i],-j*i+dp2[!z][j]-qs[pos2[j+1]-1]+1LL*pos2[j+1]*(j+1)-j+qs[i]-i-1);
}
}
}
struct line{
ll m,c;
line(ll m,ll c):m(m),c(c){}
ll operator()(const ll x)const{return m*x+c;}
};
struct cht{
deque<line> dq;
bool check(line a,line b,line c){
return (a.c-b.c)*(c.m-a.m)>=(a.c-c.c)*(b.m-a.m);
}
void insert(ll m,ll c){
line f(m,c);
while(dq.size()>1&&check(dq.end()[-2],dq.back(),f)) dq.pop_back();
dq.eb(f);
}
ll qr(ll x){
if(dq.empty()) return inf;
while(dq.size()>1&&dq[1](x)<dq.front()(x)) dq.pop_front();
return dq.front()(x);
}
void clear(){dq.clear();}
}dp;
void play(int n,bool z){
dp.clear();
int cur=0;
for(int i=1;i<=n;++i){
while(cur<i&&pos2[cur+1]<=i){
dp.insert(-cur,dp2[!z][cur]-qs[pos2[cur+1]-1]+1LL*pos2[cur+1]*(cur+1)-cur);
++cur;
}
dp2[z][i]=dp.qr(i)+qs[i]-i-1;
if(cur<i) dp2[z][i]=min(dp2[z][i],dp2[!z][cur]);
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n,K; cin>>n>>K;
string s; cin>>s;
for(int i=0,ia=0,ib=0;i<s.length();++i){
if(s[i]=='A') a[++ia]=i;
else b[++ib]=i;
}
for(int i=1,j=1;i<=n;++i){
while(j<=n&&b[j]<a[i]) ++j;
pos[i]=j;
qs[i]=qs[i-1]+j;
}
for(int i=1,j=1;i<=n;++i){
while(j<=n&&pos[j]<=i) ++j;
j=max(i,j);
pos2[i]=j;
}
for(int i=1;i<=n;++i) dp2[1][i]=qs[i]-i;
for(int k=2;k<=K;++k){
// play(1,n,1,n,k&1);
play(n,k&1);
}
cout<<dp2[K&1][n]<<endl;
return 0;
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:72:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i=0,ia=0,ib=0;i<s.length();++i){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12756 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12772 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12756 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12772 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
129 ms |
12636 KB |
Output is correct |
30 |
Correct |
413 ms |
12852 KB |
Output is correct |
31 |
Correct |
101 ms |
12632 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
12636 KB |
Output is correct |
34 |
Correct |
281 ms |
12860 KB |
Output is correct |
35 |
Correct |
298 ms |
12888 KB |
Output is correct |
36 |
Correct |
5 ms |
12636 KB |
Output is correct |
37 |
Correct |
128 ms |
12860 KB |
Output is correct |
38 |
Correct |
240 ms |
12860 KB |
Output is correct |
39 |
Correct |
218 ms |
12632 KB |
Output is correct |
40 |
Correct |
106 ms |
12836 KB |
Output is correct |
41 |
Correct |
105 ms |
12636 KB |
Output is correct |
42 |
Correct |
65 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12756 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12772 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
129 ms |
12636 KB |
Output is correct |
30 |
Correct |
413 ms |
12852 KB |
Output is correct |
31 |
Correct |
101 ms |
12632 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
12636 KB |
Output is correct |
34 |
Correct |
281 ms |
12860 KB |
Output is correct |
35 |
Correct |
298 ms |
12888 KB |
Output is correct |
36 |
Correct |
5 ms |
12636 KB |
Output is correct |
37 |
Correct |
128 ms |
12860 KB |
Output is correct |
38 |
Correct |
240 ms |
12860 KB |
Output is correct |
39 |
Correct |
218 ms |
12632 KB |
Output is correct |
40 |
Correct |
106 ms |
12836 KB |
Output is correct |
41 |
Correct |
105 ms |
12636 KB |
Output is correct |
42 |
Correct |
65 ms |
12632 KB |
Output is correct |
43 |
Execution timed out |
7024 ms |
15448 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12756 KB |
Output is correct |
19 |
Correct |
4 ms |
12632 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
5 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
5 ms |
12772 KB |
Output is correct |
26 |
Correct |
4 ms |
12636 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12632 KB |
Output is correct |
29 |
Correct |
129 ms |
12636 KB |
Output is correct |
30 |
Correct |
413 ms |
12852 KB |
Output is correct |
31 |
Correct |
101 ms |
12632 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
12636 KB |
Output is correct |
34 |
Correct |
281 ms |
12860 KB |
Output is correct |
35 |
Correct |
298 ms |
12888 KB |
Output is correct |
36 |
Correct |
5 ms |
12636 KB |
Output is correct |
37 |
Correct |
128 ms |
12860 KB |
Output is correct |
38 |
Correct |
240 ms |
12860 KB |
Output is correct |
39 |
Correct |
218 ms |
12632 KB |
Output is correct |
40 |
Correct |
106 ms |
12836 KB |
Output is correct |
41 |
Correct |
105 ms |
12636 KB |
Output is correct |
42 |
Correct |
65 ms |
12632 KB |
Output is correct |
43 |
Execution timed out |
7024 ms |
15448 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |