#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define isz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)
struct Line{
mutable int k, m, p, cnt;
bool operator<(const Line &a){ return k<a.k; }
bool operator<(int x){ return p<x; }
};
const int inf=1e18;
struct LineContainer: vector<Line>{
int id;
int div(int a, int b){
return a/b-((a^b)<0 && a%b);
}
bool isect(iterator x, iterator y){
if (y==end()) return x->p=inf, 0;
if (x->k==y->k) x->p=(x->k>=y->k)?inf:-inf;
else x->p=div(y->m-x->m, x->k-y->k);
return x->p>=y->p;
}
void add(int k, int m, int cnt){
auto z=insert(end(), {k, m, 0, cnt}), y=z++, x=y;
while (isect(y, z)) z=erase(z);
if (x!=begin() && isect(--x, y)) isect(x, erase(y));
while ((y=x)!=begin() && (--x)->p>=y->p) isect(x, erase(y));
}
LineContainer(){
id=0;
}
pair<int, int> query(int x){
while ((begin()+id)->operator<(x)) ++id;
return {(begin()+id)->k*x+(begin()+id)->m, (begin()+id)->cnt};
}
};
const int N=1e6+10;
int n, k, a[N], pf[N];
pair<int, int> f[N];
string s;
pair<int, int> dp(int penalty){
LineContainer cht;
for (int i=1; i<=n; ++i) f[i]={inf, 0};
f[0]={0, 0};
cht.add(0, 0, 0);
for (int i=1, j=0; i<=n; ++i){
while (j<i && a[j+1]<i) ++j;
auto tmp=cht.query(i);
f[i]={max(0ll, -tmp.first+j*i-pf[j])+penalty, tmp.second+1};
cht.add(i, -f[i].first-pf[i], f[i].second);
}
return f[n];
}
void solve(){
cin >> n >> k >> s;
for (int i=0, j=1; i<n*2; ++i){
if (s[i]=='A') ++a[j];
else a[j+1]=a[j], ++j;
}
partial_sum(a, a+n+1, pf);
dp(0);
int l=0, r=1e9;
while (l<=r){
int mid=(l+r)>>1;
auto tmp=dp(mid);
if (tmp.second<=k) r=mid-1;
else l=mid+1;
}
auto ans=dp(l);
cout << ans.first-l*k;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int ntests=1;
// cin >> ntests;
for (int i=1; i<=ntests; ++i) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2908 KB |
Output is correct |
30 |
Correct |
6 ms |
2868 KB |
Output is correct |
31 |
Correct |
6 ms |
2876 KB |
Output is correct |
32 |
Correct |
5 ms |
2904 KB |
Output is correct |
33 |
Correct |
7 ms |
2908 KB |
Output is correct |
34 |
Correct |
6 ms |
3092 KB |
Output is correct |
35 |
Correct |
6 ms |
3076 KB |
Output is correct |
36 |
Correct |
6 ms |
2652 KB |
Output is correct |
37 |
Correct |
7 ms |
2876 KB |
Output is correct |
38 |
Correct |
6 ms |
3076 KB |
Output is correct |
39 |
Correct |
6 ms |
2908 KB |
Output is correct |
40 |
Correct |
5 ms |
2652 KB |
Output is correct |
41 |
Correct |
5 ms |
2720 KB |
Output is correct |
42 |
Correct |
5 ms |
2652 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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2908 KB |
Output is correct |
30 |
Correct |
6 ms |
2868 KB |
Output is correct |
31 |
Correct |
6 ms |
2876 KB |
Output is correct |
32 |
Correct |
5 ms |
2904 KB |
Output is correct |
33 |
Correct |
7 ms |
2908 KB |
Output is correct |
34 |
Correct |
6 ms |
3092 KB |
Output is correct |
35 |
Correct |
6 ms |
3076 KB |
Output is correct |
36 |
Correct |
6 ms |
2652 KB |
Output is correct |
37 |
Correct |
7 ms |
2876 KB |
Output is correct |
38 |
Correct |
6 ms |
3076 KB |
Output is correct |
39 |
Correct |
6 ms |
2908 KB |
Output is correct |
40 |
Correct |
5 ms |
2652 KB |
Output is correct |
41 |
Correct |
5 ms |
2720 KB |
Output is correct |
42 |
Correct |
5 ms |
2652 KB |
Output is correct |
43 |
Correct |
82 ms |
7364 KB |
Output is correct |
44 |
Correct |
143 ms |
9608 KB |
Output is correct |
45 |
Correct |
147 ms |
9824 KB |
Output is correct |
46 |
Incorrect |
94 ms |
10892 KB |
Output isn't correct |
47 |
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 |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2516 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2648 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
5 ms |
2908 KB |
Output is correct |
30 |
Correct |
6 ms |
2868 KB |
Output is correct |
31 |
Correct |
6 ms |
2876 KB |
Output is correct |
32 |
Correct |
5 ms |
2904 KB |
Output is correct |
33 |
Correct |
7 ms |
2908 KB |
Output is correct |
34 |
Correct |
6 ms |
3092 KB |
Output is correct |
35 |
Correct |
6 ms |
3076 KB |
Output is correct |
36 |
Correct |
6 ms |
2652 KB |
Output is correct |
37 |
Correct |
7 ms |
2876 KB |
Output is correct |
38 |
Correct |
6 ms |
3076 KB |
Output is correct |
39 |
Correct |
6 ms |
2908 KB |
Output is correct |
40 |
Correct |
5 ms |
2652 KB |
Output is correct |
41 |
Correct |
5 ms |
2720 KB |
Output is correct |
42 |
Correct |
5 ms |
2652 KB |
Output is correct |
43 |
Correct |
82 ms |
7364 KB |
Output is correct |
44 |
Correct |
143 ms |
9608 KB |
Output is correct |
45 |
Correct |
147 ms |
9824 KB |
Output is correct |
46 |
Incorrect |
94 ms |
10892 KB |
Output isn't correct |
47 |
Halted |
0 ms |
0 KB |
- |