QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#487647 | #6338. Chorus | Unforgettablepl | 40 | 308ms | 3736kb | C++20 | 2.2kb | 2024-07-23 04:30:05 | 2024-07-23 04:30:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
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;
}
}
auto solvemax = [&](int penalty){
vector<pair<int,int>> DP(n+1,{INF,-INF});
DP[0]={0,0};
for(int i=1;i<=n;i++){
fenwick addedA(2*n);
fenwick addedB(2*n);
int ans = penalty;
for(int j=i+1;j<=n;j++)addedB.add(posB[j],1);
for(int j=i;j;j--){
ans+=addedA.get(2*n)-addedA.get(posB[j]);
addedB.add(posB[j],1);
ans+=addedB.get(posA[j]);
addedA.add(posA[j],1);
DP[i]=min(DP[i],{DP[j-1].first+ans,DP[j-1].second-1});
}
}
DP[n].second*=-1ll;
return DP[n];
};
auto solvemin = [&](int penalty){
vector<pair<int,int>> DP(n+1,{INF,-INF});
DP[0]={0,0};
for(int i=1;i<=n;i++){
fenwick addedA(2*n);
fenwick addedB(2*n);
int ans = penalty;
for(int j=i+1;j<=n;j++)addedB.add(posB[j],1);
for(int j=i;j;j--){
ans+=addedA.get(2*n)-addedA.get(posB[j]);
addedB.add(posB[j],1);
ans+=addedB.get(posA[j]);
addedA.add(posA[j],1);
DP[i]=min(DP[i],{DP[j-1].first+ans,DP[j-1].second+1});
}
}
return DP[n];
};
auto solve = [&](int penalty){
auto mini = solvemin(penalty);
auto maxi = solvemax(penalty);
if(mini.second>k)return mini;
if(maxi.second<=k)return maxi;
return make_pair(mini.first,k);
};
int best_penalty = -1;
for(int jump=(1ll<<50);jump;jump/=2ll){
if(solve(best_penalty+jump).second>k)best_penalty+=jump;
}
// for(int i=0;i<=10;i++)cout<<solve(i).second<<'\n';
best_penalty++;
cout << solve(best_penalty).first-solve(best_penalty).second*best_penalty << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 16
Accepted
Test #1:
score: 16
Accepted
time: 1ms
memory: 3464kb
input:
1 1 BA
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 16
Accepted
time: 1ms
memory: 3560kb
input:
7 5 ABBAAABBABABBA
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 16
Accepted
time: 1ms
memory: 3540kb
input:
10 3 BABBABAABAABBABBBAAA
output:
26
result:
ok 1 number(s): "26"
Test #4:
score: 16
Accepted
time: 1ms
memory: 3476kb
input:
10 2 AAABBABABBAAABBBAABB
output:
11
result:
ok 1 number(s): "11"
Test #5:
score: 16
Accepted
time: 1ms
memory: 3484kb
input:
10 1 BBBBBBBBBBAAAAAAAAAA
output:
100
result:
ok 1 number(s): "100"
Test #6:
score: 16
Accepted
time: 1ms
memory: 3468kb
input:
10 2 BBBBBBBBBBAAAAAAAAAA
output:
75
result:
ok 1 number(s): "75"
Test #7:
score: 16
Accepted
time: 1ms
memory: 3532kb
input:
10 9 BBBBBBBBBBAAAAAAAAAA
output:
56
result:
ok 1 number(s): "56"
Test #8:
score: 16
Accepted
time: 1ms
memory: 3412kb
input:
10 10 BBBBBBBBBBAAAAAAAAAA
output:
55
result:
ok 1 number(s): "55"
Test #9:
score: 16
Accepted
time: 1ms
memory: 3472kb
input:
10 10 ABABABABABABABABABAB
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 16
Accepted
time: 1ms
memory: 3736kb
input:
10 2 ABAAABABABBBABABABAB
output:
14
result:
ok 1 number(s): "14"
Test #11:
score: 16
Accepted
time: 1ms
memory: 3532kb
input:
10 4 ABAABBAAABBBAAABBBAB
output:
2
result:
ok 1 number(s): "2"
Test #12:
score: 16
Accepted
time: 1ms
memory: 3540kb
input:
10 4 ABAAABBBAAABBBAABBAB
output:
2
result:
ok 1 number(s): "2"
Subtask #2:
score: 24
Accepted
Dependency #1:
100%
Accepted
Test #13:
score: 24
Accepted
time: 31ms
memory: 3496kb
input:
179 54 AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...
output:
41
result:
ok 1 number(s): "41"
Test #14:
score: 24
Accepted
time: 255ms
memory: 3712kb
input:
500 93 ABABAABBBBABAABAABAAAAABABBBBBABABBABAAAABAABBBBABAABBBAAABBABABAAAABAABBABAAABBAAABBBABBAAAAAABABABBBABABBBAABAAABAABABAAABAAABBAAABABBBAABBABBBABBBAABAAAABBBAABBBABAAAAAABABABBBABAAAABBBBBAABBBABAAABAABBBABABAABABBBBABBABBBBBBBABAAAABAABAABBABBBAABBBAAAAAAABBAABAAABBBABBBAAABABBBBAABBBABABB...
output:
235
result:
ok 1 number(s): "235"
Test #15:
score: 24
Accepted
time: 264ms
memory: 3520kb
input:
500 273 AAABBABAABAAABABAABBBABAAAABBAAABBABAAABBABABAAAABBBAAABBBAABAABAABBABABABABBAAABBBBBBBAABABBAABABBBAABBBBAAABAAAABABBABBBBBAABAABABAAAABBABABABAAAABBBAAAAABABBAABABABAAABBABAAAABABBBAAABABAABBAABBBAABAABBBAABABBBBABAAABAABBBBABBABAAABABBAABABBABBBBBBBABAABAAAABABABAAABABABABAABBAABBBABBBAAB...
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 24
Accepted
time: 262ms
memory: 3552kb
input:
500 1 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
250000
result:
ok 1 number(s): "250000"
Test #17:
score: 24
Accepted
time: 267ms
memory: 3588kb
input:
500 2 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
187500
result:
ok 1 number(s): "187500"
Test #18:
score: 24
Accepted
time: 266ms
memory: 3592kb
input:
500 499 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
125251
result:
ok 1 number(s): "125251"
Test #19:
score: 24
Accepted
time: 262ms
memory: 3596kb
input:
500 500 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
125250
result:
ok 1 number(s): "125250"
Test #20:
score: 24
Accepted
time: 308ms
memory: 3500kb
input:
500 10 ABAAABBBAABBABABAABAABABBBAABABABBAABABAABAABABABBBBABAABBAAAAAAABAABAABAABBABBABABBBABABBAABABBBBAABBBBABAAAAAAABAABAAABABABBAABABBABBAAAABAABBBAABABBABABBBBAAAABABBAAAAABAABBBBBBBBABBABBBAABBABBAAAABAABBBBBBAABBBAAAABABBAAABAABAABBBBAABBAAABBAABBAAAAABAAAAAABBAABABBABABBBABAAABBBAAABABAAABB...
output:
9129
result:
ok 1 number(s): "9129"
Test #21:
score: 24
Accepted
time: 243ms
memory: 3592kb
input:
500 500 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
0
result:
ok 1 number(s): "0"
Test #22:
score: 24
Accepted
time: 245ms
memory: 3692kb
input:
500 416 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
84
result:
ok 1 number(s): "84"
Test #23:
score: 24
Accepted
time: 267ms
memory: 3516kb
input:
499 167 ABAABBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAAB...
output:
2
result:
ok 1 number(s): "2"
Test #24:
score: 24
Accepted
time: 256ms
memory: 3556kb
input:
499 167 ABAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAA...
output:
2
result:
ok 1 number(s): "2"
Subtask #3:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #25:
score: 0
Time Limit Exceeded
input:
4918 2048 ABBBBBBAAAAABBBBAAAABBABBAAAABBBAABBBABABBAAAABBBAAAABAABBAAABAAABAABBAAABAAABBBAABBBBBBABAABBBBAAAAABBBBBABBBABABBBBBABAAAAAABAABBABBABBBBBAABAABAAAAAABBBBBAABBBABBBAAABBABBABBBAABBBBBBABBBABAABABABABABAAABBABABBAABAABBBBABABBAABABABABBAABABABBABABABAAABABBBABBBABBABBBBAAAABBBBAAABBBABBBA...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%