#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e13;
struct fenwick{
vector<int> tree;
int n;
fenwick(int n):n(n),tree(n+1){}
void add(int k,int x){
while(k<=n){
tree[k]+=x;
k+=k&-k;
}
}
int get(int k){
int ans = 0;
while(k){
ans+=tree[k];
k-=k&-k;
}
return ans;
}
};
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,k;
cin >> n >> k;
string s;
cin >> s;
s.insert(s.begin(),'$');
vector<int> posA(n+1);
vector<int> posB(n+1);
int cntA = 0,cntB = 0;
for(int i=1;i<=2*n;i++){
if(s[i]=='A'){
posA[++cntA]=i;
} else {
posB[++cntB]=i;
}
}
vector<int> costA(n+1);
vector<int> costB(n+1);
vector<int> lastB(n+1);
{
fenwick temp(2*n);
for(int i=n;i;i--){
temp.add(posB[i],1);
costA[i] = temp.get(posA[i]);
}
for(int i=1;i<=n;i++)costA[i]+=costA[i-1];
}
{
fenwick temp(2*n);
for(int i=n;i;i--){
costB[i] = n-i-temp.get(posB[i]);
temp.add(posA[i],1);
}
for(int i=1;i<=n;i++)costB[i]+=costB[i-1];
}
{
int currlast = 0;
for(int i=1;i<=n;i++){
while(currlast!=n and posB[currlast+1]<posA[i])currlast++;
lastB[i] = min(currlast,i);
}
}
auto solve = [&](int penalty){
vector<pair<int,int>> DP(n+1,{INF,INF});
DP[0]={0,0};
auto cost = [&](int a,int b){
assert(a<=b);
int Ac = costA[b]-costA[a-1];
if(lastB[b]<a)return Ac+penalty;
int amt = lastB[b]-a+1;;
int Bc = costB[lastB[b]]-costB[a-1]-amt*(n-b);
return Ac+Bc+penalty;
};
deque<pair<int,int>> q;
auto test = [&](int x,int a,int b){
// Will return true if a is better for x than b
return make_pair(DP[a-1].first+cost(a,x),DP[a-1].second+1)<make_pair(DP[b-1].first+cost(b,x),DP[b-1].second+1);
};
for(int i=1;i<=n;i++){
// Add i
while(!q.empty() and q.front().second<i)q.pop_front();
while(!q.empty()){
int lower = i;
if(q.size()>1)lower = q[q.size()-2].second+1;
if(test(lower,i,q.back().first)){
q.pop_back();
continue;
}
for(int jump=524288;jump;jump/=2)if(lower+jump<=n and test(lower+jump,q.back().first,i))lower+=jump;
q.back().second = lower;
break;
}
if(q.empty() or q.back().second!=n)q.emplace_back(i,n);
// Query i
assert(!q.empty());
DP[i] = {DP[q.front().first-1].first+cost(q.front().first,i),DP[q.front().first-1].second+1};
}
return DP[n];
};
int best_penalty = -1;
for(int jump=(1ll<<40);jump;jump/=2ll){
if(solve(best_penalty+jump).second>k)best_penalty+=jump;
}
best_penalty++;
cout << solve(best_penalty).first-k*best_penalty << '\n';
}
Compilation message
chorus.cpp: In constructor 'fenwick::fenwick(long long int)':
chorus.cpp:10:6: warning: 'fenwick::n' will be initialized after [-Wreorder]
10 | int n;
| ^
chorus.cpp:9:14: warning: 'std::vector<long long int> fenwick::tree' [-Wreorder]
9 | vector<int> tree;
| ^~~~
chorus.cpp:11:2: warning: when initialized here [-Wreorder]
11 | fenwick(int n):n(n),tree(n+1){}
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
448 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
448 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
448 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
768 KB |
Output is correct |
30 |
Correct |
15 ms |
772 KB |
Output is correct |
31 |
Correct |
14 ms |
600 KB |
Output is correct |
32 |
Correct |
12 ms |
604 KB |
Output is correct |
33 |
Correct |
14 ms |
784 KB |
Output is correct |
34 |
Correct |
13 ms |
604 KB |
Output is correct |
35 |
Correct |
12 ms |
760 KB |
Output is correct |
36 |
Correct |
15 ms |
604 KB |
Output is correct |
37 |
Correct |
15 ms |
780 KB |
Output is correct |
38 |
Correct |
12 ms |
604 KB |
Output is correct |
39 |
Correct |
15 ms |
604 KB |
Output is correct |
40 |
Correct |
13 ms |
720 KB |
Output is correct |
41 |
Correct |
12 ms |
600 KB |
Output is correct |
42 |
Correct |
12 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
448 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
768 KB |
Output is correct |
30 |
Correct |
15 ms |
772 KB |
Output is correct |
31 |
Correct |
14 ms |
600 KB |
Output is correct |
32 |
Correct |
12 ms |
604 KB |
Output is correct |
33 |
Correct |
14 ms |
784 KB |
Output is correct |
34 |
Correct |
13 ms |
604 KB |
Output is correct |
35 |
Correct |
12 ms |
760 KB |
Output is correct |
36 |
Correct |
15 ms |
604 KB |
Output is correct |
37 |
Correct |
15 ms |
780 KB |
Output is correct |
38 |
Correct |
12 ms |
604 KB |
Output is correct |
39 |
Correct |
15 ms |
604 KB |
Output is correct |
40 |
Correct |
13 ms |
720 KB |
Output is correct |
41 |
Correct |
12 ms |
600 KB |
Output is correct |
42 |
Correct |
12 ms |
604 KB |
Output is correct |
43 |
Correct |
209 ms |
4016 KB |
Output is correct |
44 |
Correct |
367 ms |
6604 KB |
Output is correct |
45 |
Correct |
380 ms |
6588 KB |
Output is correct |
46 |
Correct |
326 ms |
6376 KB |
Output is correct |
47 |
Correct |
297 ms |
6608 KB |
Output is correct |
48 |
Correct |
356 ms |
6584 KB |
Output is correct |
49 |
Correct |
393 ms |
6592 KB |
Output is correct |
50 |
Correct |
362 ms |
6684 KB |
Output is correct |
51 |
Correct |
371 ms |
6592 KB |
Output is correct |
52 |
Correct |
346 ms |
6588 KB |
Output is correct |
53 |
Correct |
361 ms |
6588 KB |
Output is correct |
54 |
Correct |
444 ms |
6848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
456 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
448 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
452 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
768 KB |
Output is correct |
30 |
Correct |
15 ms |
772 KB |
Output is correct |
31 |
Correct |
14 ms |
600 KB |
Output is correct |
32 |
Correct |
12 ms |
604 KB |
Output is correct |
33 |
Correct |
14 ms |
784 KB |
Output is correct |
34 |
Correct |
13 ms |
604 KB |
Output is correct |
35 |
Correct |
12 ms |
760 KB |
Output is correct |
36 |
Correct |
15 ms |
604 KB |
Output is correct |
37 |
Correct |
15 ms |
780 KB |
Output is correct |
38 |
Correct |
12 ms |
604 KB |
Output is correct |
39 |
Correct |
15 ms |
604 KB |
Output is correct |
40 |
Correct |
13 ms |
720 KB |
Output is correct |
41 |
Correct |
12 ms |
600 KB |
Output is correct |
42 |
Correct |
12 ms |
604 KB |
Output is correct |
43 |
Correct |
209 ms |
4016 KB |
Output is correct |
44 |
Correct |
367 ms |
6604 KB |
Output is correct |
45 |
Correct |
380 ms |
6588 KB |
Output is correct |
46 |
Correct |
326 ms |
6376 KB |
Output is correct |
47 |
Correct |
297 ms |
6608 KB |
Output is correct |
48 |
Correct |
356 ms |
6584 KB |
Output is correct |
49 |
Correct |
393 ms |
6592 KB |
Output is correct |
50 |
Correct |
362 ms |
6684 KB |
Output is correct |
51 |
Correct |
371 ms |
6592 KB |
Output is correct |
52 |
Correct |
346 ms |
6588 KB |
Output is correct |
53 |
Correct |
361 ms |
6588 KB |
Output is correct |
54 |
Correct |
444 ms |
6848 KB |
Output is correct |
55 |
Correct |
2831 ms |
40300 KB |
Output is correct |
56 |
Correct |
4676 ms |
62856 KB |
Output is correct |
57 |
Correct |
4490 ms |
62760 KB |
Output is correct |
58 |
Correct |
3436 ms |
61220 KB |
Output is correct |
59 |
Correct |
3457 ms |
63016 KB |
Output is correct |
60 |
Correct |
4671 ms |
62980 KB |
Output is correct |
61 |
Correct |
4541 ms |
62980 KB |
Output is correct |
62 |
Correct |
4571 ms |
62588 KB |
Output is correct |
63 |
Correct |
5084 ms |
62760 KB |
Output is correct |
64 |
Correct |
4937 ms |
63016 KB |
Output is correct |
65 |
Correct |
5077 ms |
63020 KB |
Output is correct |
66 |
Correct |
5419 ms |
62844 KB |
Output is correct |
67 |
Correct |
4848 ms |
63100 KB |
Output is correct |