#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define int long long
const int mod=1e9+7,mxn=5005,lg=30,inf=1e18,minf=-1e18,Mxn=2e6,root=700;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int dp[mxn+10][mxn+10];
int cost[mxn+10][mxn+10];
int pref[mxn*2+10];
vector<int>A,B;
int n,k;
string a;
void getcost(int st){
int curcost=0;
for(int i=A[st];i<=n*2;i++){
if(a[i]=='A')pref[i]++;
pref[i]+=pref[i-1];
}
for(int i=st;i<=n;i++){
cost[st][i]=cost[st][i-1]+pref[B[i]];
}
for(int i=A[st];i<=2*n;i++)pref[i]=0;
/*
when introduced new B
*/
}
void dc(int l,int r,int optl,int optr,int k){
if(r<l)return;
int mid=l+(r-l)/2;
pii ans={inf,n};
for(int i=optl;i<=min(optr,mid-1);i++){
ans=min(ans,make_pair(dp[i][k-1]+cost[i+1][mid],i));
}
dp[mid][k]=ans.f;
dc(l,mid-1,optl,ans.s,k);
dc(mid+1,r,ans.s,optr,k);
}
int32_t main(){
fastio
cin>>n>>k;
cin>>a;
reverse(all(a));
a='G'+a;
A.pb(69);
B.pb(420);
for(int i=1;i<=n*2;i++){
if(a[i]=='A')A.pb(i);
else B.pb(i);
}
for(int i=1;i<=n;i++)getcost(i);
for(int i=0;i<=k;i++)for(int j=1;j<=n;j++)dp[j][i]=inf;
dp[0][0]=0;
for(int i=1;i<=k;i++){//choose k use pref j
dc(1,n,0,n,i);/*
for(int j=1;j<=n;j++){
for(int g=0;g<j;g++){
dp[j][i]=min(dp[j][i],dp[g][i-1]+cost[g+1][j]);
}
}*/
}
cout<<dp[n][k];
//can op to (n^2logn)? dac dp?
//too lazy to prove :((
}
Compilation message
chorus.cpp: In function 'void getcost(long long int)':
chorus.cpp:47:6: warning: unused variable 'curcost' [-Wunused-variable]
47 | int curcost=0;
| ^~~~~~~
chorus.cpp: In function 'void setIO(std::string)':
chorus.cpp:37:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chorus.cpp:38:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4600 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4600 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
8 ms |
25948 KB |
Output is correct |
19 |
Correct |
7 ms |
25948 KB |
Output is correct |
20 |
Correct |
5 ms |
25944 KB |
Output is correct |
21 |
Correct |
5 ms |
25944 KB |
Output is correct |
22 |
Correct |
9 ms |
25944 KB |
Output is correct |
23 |
Correct |
10 ms |
25944 KB |
Output is correct |
24 |
Correct |
5 ms |
25944 KB |
Output is correct |
25 |
Correct |
8 ms |
25948 KB |
Output is correct |
26 |
Correct |
9 ms |
25944 KB |
Output is correct |
27 |
Correct |
7 ms |
25948 KB |
Output is correct |
28 |
Correct |
6 ms |
25824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4600 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
8 ms |
25948 KB |
Output is correct |
19 |
Correct |
7 ms |
25948 KB |
Output is correct |
20 |
Correct |
5 ms |
25944 KB |
Output is correct |
21 |
Correct |
5 ms |
25944 KB |
Output is correct |
22 |
Correct |
9 ms |
25944 KB |
Output is correct |
23 |
Correct |
10 ms |
25944 KB |
Output is correct |
24 |
Correct |
5 ms |
25944 KB |
Output is correct |
25 |
Correct |
8 ms |
25948 KB |
Output is correct |
26 |
Correct |
9 ms |
25944 KB |
Output is correct |
27 |
Correct |
7 ms |
25948 KB |
Output is correct |
28 |
Correct |
6 ms |
25824 KB |
Output is correct |
29 |
Correct |
651 ms |
220764 KB |
Output is correct |
30 |
Correct |
1677 ms |
315368 KB |
Output is correct |
31 |
Correct |
448 ms |
196688 KB |
Output is correct |
32 |
Correct |
113 ms |
155384 KB |
Output is correct |
33 |
Correct |
111 ms |
154192 KB |
Output is correct |
34 |
Correct |
1423 ms |
331356 KB |
Output is correct |
35 |
Correct |
1408 ms |
330324 KB |
Output is correct |
36 |
Correct |
103 ms |
155728 KB |
Output is correct |
37 |
Correct |
444 ms |
194524 KB |
Output is correct |
38 |
Correct |
1172 ms |
315972 KB |
Output is correct |
39 |
Correct |
811 ms |
248580 KB |
Output is correct |
40 |
Correct |
526 ms |
220756 KB |
Output is correct |
41 |
Correct |
558 ms |
219372 KB |
Output is correct |
42 |
Correct |
1277 ms |
220716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4600 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
8 ms |
25948 KB |
Output is correct |
19 |
Correct |
7 ms |
25948 KB |
Output is correct |
20 |
Correct |
5 ms |
25944 KB |
Output is correct |
21 |
Correct |
5 ms |
25944 KB |
Output is correct |
22 |
Correct |
9 ms |
25944 KB |
Output is correct |
23 |
Correct |
10 ms |
25944 KB |
Output is correct |
24 |
Correct |
5 ms |
25944 KB |
Output is correct |
25 |
Correct |
8 ms |
25948 KB |
Output is correct |
26 |
Correct |
9 ms |
25944 KB |
Output is correct |
27 |
Correct |
7 ms |
25948 KB |
Output is correct |
28 |
Correct |
6 ms |
25824 KB |
Output is correct |
29 |
Correct |
651 ms |
220764 KB |
Output is correct |
30 |
Correct |
1677 ms |
315368 KB |
Output is correct |
31 |
Correct |
448 ms |
196688 KB |
Output is correct |
32 |
Correct |
113 ms |
155384 KB |
Output is correct |
33 |
Correct |
111 ms |
154192 KB |
Output is correct |
34 |
Correct |
1423 ms |
331356 KB |
Output is correct |
35 |
Correct |
1408 ms |
330324 KB |
Output is correct |
36 |
Correct |
103 ms |
155728 KB |
Output is correct |
37 |
Correct |
444 ms |
194524 KB |
Output is correct |
38 |
Correct |
1172 ms |
315972 KB |
Output is correct |
39 |
Correct |
811 ms |
248580 KB |
Output is correct |
40 |
Correct |
526 ms |
220756 KB |
Output is correct |
41 |
Correct |
558 ms |
219372 KB |
Output is correct |
42 |
Correct |
1277 ms |
220716 KB |
Output is correct |
43 |
Runtime error |
6091 ms |
801676 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4600 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4604 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
2 ms |
11352 KB |
Output is correct |
18 |
Correct |
8 ms |
25948 KB |
Output is correct |
19 |
Correct |
7 ms |
25948 KB |
Output is correct |
20 |
Correct |
5 ms |
25944 KB |
Output is correct |
21 |
Correct |
5 ms |
25944 KB |
Output is correct |
22 |
Correct |
9 ms |
25944 KB |
Output is correct |
23 |
Correct |
10 ms |
25944 KB |
Output is correct |
24 |
Correct |
5 ms |
25944 KB |
Output is correct |
25 |
Correct |
8 ms |
25948 KB |
Output is correct |
26 |
Correct |
9 ms |
25944 KB |
Output is correct |
27 |
Correct |
7 ms |
25948 KB |
Output is correct |
28 |
Correct |
6 ms |
25824 KB |
Output is correct |
29 |
Correct |
651 ms |
220764 KB |
Output is correct |
30 |
Correct |
1677 ms |
315368 KB |
Output is correct |
31 |
Correct |
448 ms |
196688 KB |
Output is correct |
32 |
Correct |
113 ms |
155384 KB |
Output is correct |
33 |
Correct |
111 ms |
154192 KB |
Output is correct |
34 |
Correct |
1423 ms |
331356 KB |
Output is correct |
35 |
Correct |
1408 ms |
330324 KB |
Output is correct |
36 |
Correct |
103 ms |
155728 KB |
Output is correct |
37 |
Correct |
444 ms |
194524 KB |
Output is correct |
38 |
Correct |
1172 ms |
315972 KB |
Output is correct |
39 |
Correct |
811 ms |
248580 KB |
Output is correct |
40 |
Correct |
526 ms |
220756 KB |
Output is correct |
41 |
Correct |
558 ms |
219372 KB |
Output is correct |
42 |
Correct |
1277 ms |
220716 KB |
Output is correct |
43 |
Runtime error |
6091 ms |
801676 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |