#include<bits/stdc++.h>
#define MAXN 10007
using namespace std;
const int inf=1e9;
int n,k;
int ans;
char s[MAXN];
int dp[MAXN][MAXN];
int opt[MAXN][MAXN],pref[MAXN];
int sum[MAXN][MAXN],sum2[MAXN][MAXN];
int cost(int l,int r){
if((r-l+1)%2!=0)return inf;
return (sum[(r-l+1)/2][(l+r)/2] - sum[0][(l+r)/2-(r-l+1)/2] + sum2[(r-l+1)/2-1][(l+r)/2+1])/2;
}
void solve(int l,int r,int optl,int optr,int k){
if(l>r)return;
int mid=((l+r)/2)*2;
dp[mid][k]=inf;
opt[mid][k]=optr;
for(int i=min(mid,optr);i>=optl;i--){
if(dp[i-1][k-1]+cost(i,mid)<dp[mid][k]){
dp[mid][k]=dp[i-1][k-1]+cost(i,mid);
opt[mid][k]=i;
}
}
solve(l,mid/2-1,optl,opt[mid][k],k);
solve(mid/2+1,r,opt[mid][k],optr,k);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
n*=2;
cin>>(s+1);
for(int i=1;i<=n;i++){
if(s[i]=='A')pref[i]++;
else pref[i]--;
pref[i]+=pref[i-1];
}
for(int i=0;i<=n/2;i++){
for(int f=1;f<=n;f++){
sum[i][f]=max(i-pref[f],0);
if(i>0)sum[i][f]+=sum[i-1][f-1];
sum2[i][f]=max(i-pref[f],0);
if(i>0)sum2[i][f]+=sum2[i-1][f+1];
}
}
for(int i=1;i<=n;i++){
dp[i][0]=inf;
}
ans=inf;
for(int i=1;i<=k;i++){
solve(1,n/2,1,n,i);
ans=min(ans,dp[n][i]);
}
cout<<ans<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
1 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 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 |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
1 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 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 |
6 ms |
49500 KB |
Output is correct |
18 |
Correct |
16 ms |
127580 KB |
Output is correct |
19 |
Correct |
22 ms |
127580 KB |
Output is correct |
20 |
Correct |
15 ms |
127580 KB |
Output is correct |
21 |
Correct |
14 ms |
127576 KB |
Output is correct |
22 |
Correct |
30 ms |
127748 KB |
Output is correct |
23 |
Correct |
25 ms |
127576 KB |
Output is correct |
24 |
Correct |
15 ms |
127580 KB |
Output is correct |
25 |
Correct |
27 ms |
127580 KB |
Output is correct |
26 |
Correct |
24 ms |
127576 KB |
Output is correct |
27 |
Correct |
18 ms |
123484 KB |
Output is correct |
28 |
Correct |
17 ms |
123468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
1 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 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 |
6 ms |
49500 KB |
Output is correct |
18 |
Correct |
16 ms |
127580 KB |
Output is correct |
19 |
Correct |
22 ms |
127580 KB |
Output is correct |
20 |
Correct |
15 ms |
127580 KB |
Output is correct |
21 |
Correct |
14 ms |
127576 KB |
Output is correct |
22 |
Correct |
30 ms |
127748 KB |
Output is correct |
23 |
Correct |
25 ms |
127576 KB |
Output is correct |
24 |
Correct |
15 ms |
127580 KB |
Output is correct |
25 |
Correct |
27 ms |
127580 KB |
Output is correct |
26 |
Correct |
24 ms |
127576 KB |
Output is correct |
27 |
Correct |
18 ms |
123484 KB |
Output is correct |
28 |
Correct |
17 ms |
123468 KB |
Output is correct |
29 |
Correct |
1554 ms |
526928 KB |
Output is correct |
30 |
Correct |
2879 ms |
624908 KB |
Output is correct |
31 |
Correct |
841 ms |
495728 KB |
Output is correct |
32 |
Correct |
176 ms |
455648 KB |
Output is correct |
33 |
Correct |
183 ms |
455600 KB |
Output is correct |
34 |
Correct |
3507 ms |
650744 KB |
Output is correct |
35 |
Correct |
3945 ms |
650908 KB |
Output is correct |
36 |
Correct |
192 ms |
456068 KB |
Output is correct |
37 |
Correct |
901 ms |
494480 KB |
Output is correct |
38 |
Correct |
2782 ms |
617384 KB |
Output is correct |
39 |
Correct |
1742 ms |
549088 KB |
Output is correct |
40 |
Correct |
1506 ms |
520456 KB |
Output is correct |
41 |
Correct |
1599 ms |
520428 KB |
Output is correct |
42 |
Correct |
3484 ms |
520620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
1 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 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 |
6 ms |
49500 KB |
Output is correct |
18 |
Correct |
16 ms |
127580 KB |
Output is correct |
19 |
Correct |
22 ms |
127580 KB |
Output is correct |
20 |
Correct |
15 ms |
127580 KB |
Output is correct |
21 |
Correct |
14 ms |
127576 KB |
Output is correct |
22 |
Correct |
30 ms |
127748 KB |
Output is correct |
23 |
Correct |
25 ms |
127576 KB |
Output is correct |
24 |
Correct |
15 ms |
127580 KB |
Output is correct |
25 |
Correct |
27 ms |
127580 KB |
Output is correct |
26 |
Correct |
24 ms |
127576 KB |
Output is correct |
27 |
Correct |
18 ms |
123484 KB |
Output is correct |
28 |
Correct |
17 ms |
123468 KB |
Output is correct |
29 |
Correct |
1554 ms |
526928 KB |
Output is correct |
30 |
Correct |
2879 ms |
624908 KB |
Output is correct |
31 |
Correct |
841 ms |
495728 KB |
Output is correct |
32 |
Correct |
176 ms |
455648 KB |
Output is correct |
33 |
Correct |
183 ms |
455600 KB |
Output is correct |
34 |
Correct |
3507 ms |
650744 KB |
Output is correct |
35 |
Correct |
3945 ms |
650908 KB |
Output is correct |
36 |
Correct |
192 ms |
456068 KB |
Output is correct |
37 |
Correct |
901 ms |
494480 KB |
Output is correct |
38 |
Correct |
2782 ms |
617384 KB |
Output is correct |
39 |
Correct |
1742 ms |
549088 KB |
Output is correct |
40 |
Correct |
1506 ms |
520456 KB |
Output is correct |
41 |
Correct |
1599 ms |
520428 KB |
Output is correct |
42 |
Correct |
3484 ms |
520620 KB |
Output is correct |
43 |
Runtime error |
29 ms |
348 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
1 ms |
12636 KB |
Output is correct |
10 |
Correct |
1 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12632 KB |
Output is correct |
12 |
Correct |
1 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 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 |
6 ms |
49500 KB |
Output is correct |
18 |
Correct |
16 ms |
127580 KB |
Output is correct |
19 |
Correct |
22 ms |
127580 KB |
Output is correct |
20 |
Correct |
15 ms |
127580 KB |
Output is correct |
21 |
Correct |
14 ms |
127576 KB |
Output is correct |
22 |
Correct |
30 ms |
127748 KB |
Output is correct |
23 |
Correct |
25 ms |
127576 KB |
Output is correct |
24 |
Correct |
15 ms |
127580 KB |
Output is correct |
25 |
Correct |
27 ms |
127580 KB |
Output is correct |
26 |
Correct |
24 ms |
127576 KB |
Output is correct |
27 |
Correct |
18 ms |
123484 KB |
Output is correct |
28 |
Correct |
17 ms |
123468 KB |
Output is correct |
29 |
Correct |
1554 ms |
526928 KB |
Output is correct |
30 |
Correct |
2879 ms |
624908 KB |
Output is correct |
31 |
Correct |
841 ms |
495728 KB |
Output is correct |
32 |
Correct |
176 ms |
455648 KB |
Output is correct |
33 |
Correct |
183 ms |
455600 KB |
Output is correct |
34 |
Correct |
3507 ms |
650744 KB |
Output is correct |
35 |
Correct |
3945 ms |
650908 KB |
Output is correct |
36 |
Correct |
192 ms |
456068 KB |
Output is correct |
37 |
Correct |
901 ms |
494480 KB |
Output is correct |
38 |
Correct |
2782 ms |
617384 KB |
Output is correct |
39 |
Correct |
1742 ms |
549088 KB |
Output is correct |
40 |
Correct |
1506 ms |
520456 KB |
Output is correct |
41 |
Correct |
1599 ms |
520428 KB |
Output is correct |
42 |
Correct |
3484 ms |
520620 KB |
Output is correct |
43 |
Runtime error |
29 ms |
348 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |