#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());
int main(){
int n, k;
string s;
cin>>n>>k>>s;
vi V(n+1);
int a=0, b=1;
for(char c:s){
if(c=='A'){
a++;
}
else{
V[b]=a;
b++;
}
}
/*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]<<" ";
}*/
vector<ld> dp(n+1), ile(n+1);
ld l=-2e7, r=2e7;
pair<int, ll> L,R;
for(int ii=0; ii<40; ii++){
ld m=(l+r)/2;
dp[0]=0;
ile[0]=0;
for(int i=1; i<=n; i++){
ll c=0;
dp[i]=1e18;
for(int j=i-1; j>=0; j--){
c+=max(0, i-V[j+1]);
if(dp[i]>dp[j]+c+m){
dp[i]=dp[j]+c+m;
ile[i]=ile[j]+1;
}
}
}
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<<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;
//assert(false);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
21 ms |
212 KB |
Output is correct |
19 |
Correct |
22 ms |
316 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
212 KB |
Output is correct |
22 |
Correct |
22 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
10 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
21 ms |
316 KB |
Output is correct |
27 |
Correct |
14 ms |
212 KB |
Output is correct |
28 |
Correct |
14 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
21 ms |
212 KB |
Output is correct |
19 |
Correct |
22 ms |
316 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
212 KB |
Output is correct |
22 |
Correct |
22 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
10 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
21 ms |
316 KB |
Output is correct |
27 |
Correct |
14 ms |
212 KB |
Output is correct |
28 |
Correct |
14 ms |
212 KB |
Output is correct |
29 |
Correct |
1841 ms |
480 KB |
Output is correct |
30 |
Correct |
1867 ms |
480 KB |
Output is correct |
31 |
Correct |
1865 ms |
480 KB |
Output is correct |
32 |
Correct |
110 ms |
468 KB |
Output is correct |
33 |
Correct |
183 ms |
476 KB |
Output is correct |
34 |
Correct |
1862 ms |
484 KB |
Output is correct |
35 |
Correct |
43 ms |
468 KB |
Output is correct |
36 |
Correct |
573 ms |
476 KB |
Output is correct |
37 |
Correct |
1865 ms |
468 KB |
Output is correct |
38 |
Correct |
1852 ms |
484 KB |
Output is correct |
39 |
Correct |
1867 ms |
476 KB |
Output is correct |
40 |
Correct |
1179 ms |
476 KB |
Output is correct |
41 |
Correct |
1181 ms |
476 KB |
Output is correct |
42 |
Correct |
1862 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
21 ms |
212 KB |
Output is correct |
19 |
Correct |
22 ms |
316 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
212 KB |
Output is correct |
22 |
Correct |
22 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
10 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
21 ms |
316 KB |
Output is correct |
27 |
Correct |
14 ms |
212 KB |
Output is correct |
28 |
Correct |
14 ms |
212 KB |
Output is correct |
29 |
Correct |
1841 ms |
480 KB |
Output is correct |
30 |
Correct |
1867 ms |
480 KB |
Output is correct |
31 |
Correct |
1865 ms |
480 KB |
Output is correct |
32 |
Correct |
110 ms |
468 KB |
Output is correct |
33 |
Correct |
183 ms |
476 KB |
Output is correct |
34 |
Correct |
1862 ms |
484 KB |
Output is correct |
35 |
Correct |
43 ms |
468 KB |
Output is correct |
36 |
Correct |
573 ms |
476 KB |
Output is correct |
37 |
Correct |
1865 ms |
468 KB |
Output is correct |
38 |
Correct |
1852 ms |
484 KB |
Output is correct |
39 |
Correct |
1867 ms |
476 KB |
Output is correct |
40 |
Correct |
1179 ms |
476 KB |
Output is correct |
41 |
Correct |
1181 ms |
476 KB |
Output is correct |
42 |
Correct |
1862 ms |
476 KB |
Output is correct |
43 |
Execution timed out |
7033 ms |
2644 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
21 ms |
212 KB |
Output is correct |
19 |
Correct |
22 ms |
316 KB |
Output is correct |
20 |
Correct |
2 ms |
212 KB |
Output is correct |
21 |
Correct |
7 ms |
212 KB |
Output is correct |
22 |
Correct |
22 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
10 ms |
316 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
21 ms |
316 KB |
Output is correct |
27 |
Correct |
14 ms |
212 KB |
Output is correct |
28 |
Correct |
14 ms |
212 KB |
Output is correct |
29 |
Correct |
1841 ms |
480 KB |
Output is correct |
30 |
Correct |
1867 ms |
480 KB |
Output is correct |
31 |
Correct |
1865 ms |
480 KB |
Output is correct |
32 |
Correct |
110 ms |
468 KB |
Output is correct |
33 |
Correct |
183 ms |
476 KB |
Output is correct |
34 |
Correct |
1862 ms |
484 KB |
Output is correct |
35 |
Correct |
43 ms |
468 KB |
Output is correct |
36 |
Correct |
573 ms |
476 KB |
Output is correct |
37 |
Correct |
1865 ms |
468 KB |
Output is correct |
38 |
Correct |
1852 ms |
484 KB |
Output is correct |
39 |
Correct |
1867 ms |
476 KB |
Output is correct |
40 |
Correct |
1179 ms |
476 KB |
Output is correct |
41 |
Correct |
1181 ms |
476 KB |
Output is correct |
42 |
Correct |
1862 ms |
476 KB |
Output is correct |
43 |
Execution timed out |
7033 ms |
2644 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |