QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#350371 | #6338. Chorus | zyz07 | 16 | 127ms | 16268kb | C++17 | 1.5kb | 2024-03-10 17:55:39 | 2024-03-10 17:55:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define For(Ti,Ta,Tb) for(auto Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(auto Ti=(Ta);Ti>=(Tb);--Ti)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define range(Tx) begin(Tx),end(Tx)
using ll=long long;
const int N=1e6+5;
int n,k,sum[N*2],pos[2][N],rk[N*2],cnta[N*2],cntb[N*2],mark[N*2],nxt[N*2];
ll cost[N*2],f[N*2];
char s[N*2];
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>k>>(s+1);
ll ans=0;
For(i,1,n*2){
sum[i]=sum[i-1]+(s[i]=='A'?1:-1);
}
For(i,1,n*2){
if(sum[i]<0){
ans+=(-sum[i]+1)/2;
sum[i]=(sum[i]%2?1:0);
}
}
debug("ans=%lld\n",ans);
for(int i=1,cnt[2]{};i<=n*2;++i){
s[i]=(sum[i-1]<sum[i]?'A':'B');
pos[s[i]=='B'][++cnt[s[i]=='B']]=i;
rk[i]=cnt[s[i]=='B'];
}
mark[0]=1;
int len=0;
for(int i=1,pre=0;i<=n*2;){
++len;
int r=i;
for(;r<=n*2&&s[r]=='B';++r);
if(r>n*2) break;
r=pos[1][rk[r]];
for(;s[r]=='B';--r);
mark[r]=1;
nxt[pre]=r;
pre=r;
For(j,i,r){
cnta[j]=cnta[j-1]+(s[j]=='A');
cntb[j]=cntb[j-1]+(s[j]=='B');
cost[j]=cost[j-1]+(s[j]=='A')*cntb[j];
}
i=r+1;
}
k=min(k,len);
fill(f,f+n*2+1,1e18);
f[0]=0;
For($,1,k){
Dec(i,n*2,1){
if(!mark[i]) continue;
For(j,0,i-1){
if(mark[j]){
f[i]=min(f[i],f[j]+cost[i]-cost[nxt[j]]-1LL*cntb[nxt[j]]*(cnta[i]-cnta[nxt[j]]));
}
}
}
}
int pos=n*2;
for(;s[pos]=='B';--pos);
cout<<ans+f[pos]<<'\n';
return 0;
}
詳細信息
Subtask #1:
score: 16
Accepted
Test #1:
score: 16
Accepted
time: 3ms
memory: 16084kb
input:
1 1 BA
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 16096kb
input:
7 5 ABBAAABBABABBA
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 2ms
memory: 16152kb
input:
10 3 BABBABAABAABBABBBAAA
output:
26
result:
ok 1 number(s): "26"
Test #4:
score: 0
Accepted
time: 2ms
memory: 16148kb
input:
10 2 AAABBABABBAAABBBAABB
output:
11
result:
ok 1 number(s): "11"
Test #5:
score: 0
Accepted
time: 0ms
memory: 16140kb
input:
10 1 BBBBBBBBBBAAAAAAAAAA
output:
100
result:
ok 1 number(s): "100"
Test #6:
score: 0
Accepted
time: 3ms
memory: 16076kb
input:
10 2 BBBBBBBBBBAAAAAAAAAA
output:
75
result:
ok 1 number(s): "75"
Test #7:
score: 0
Accepted
time: 0ms
memory: 16136kb
input:
10 9 BBBBBBBBBBAAAAAAAAAA
output:
56
result:
ok 1 number(s): "56"
Test #8:
score: 0
Accepted
time: 2ms
memory: 16268kb
input:
10 10 BBBBBBBBBBAAAAAAAAAA
output:
55
result:
ok 1 number(s): "55"
Test #9:
score: 0
Accepted
time: 0ms
memory: 16124kb
input:
10 10 ABABABABABABABABABAB
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 0
Accepted
time: 1ms
memory: 14080kb
input:
10 2 ABAAABABABBBABABABAB
output:
14
result:
ok 1 number(s): "14"
Test #11:
score: 0
Accepted
time: 0ms
memory: 16080kb
input:
10 4 ABAABBAAABBBAAABBBAB
output:
2
result:
ok 1 number(s): "2"
Test #12:
score: 0
Accepted
time: 2ms
memory: 16136kb
input:
10 4 ABAAABBBAAABBBAABBAB
output:
2
result:
ok 1 number(s): "2"
Subtask #2:
score: 0
Wrong Answer
Dependency #1:
100%
Accepted
Test #13:
score: 24
Accepted
time: 0ms
memory: 16068kb
input:
179 54 AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...
output:
41
result:
ok 1 number(s): "41"
Test #14:
score: 0
Accepted
time: 4ms
memory: 16096kb
input:
500 93 ABABAABBBBABAABAABAAAAABABBBBBABABBABAAAABAABBBBABAABBBAAABBABABAAAABAABBABAAABBAAABBBABBAAAAAABABABBBABABBBAABAAABAABABAAABAAABBAAABABBBAABBABBBABBBAABAAAABBBAABBBABAAAAAABABABBBABAAAABBBBBAABBBABAAABAABBBABABAABABBBBABBABBBBBBBABAAAABAABAABBABBBAABBBAAAAAAABBAABAAABBBABBBAAABABBBBAABBBABABB...
output:
235
result:
ok 1 number(s): "235"
Test #15:
score: 0
Accepted
time: 0ms
memory: 16096kb
input:
500 273 AAABBABAABAAABABAABBBABAAAABBAAABBABAAABBABABAAAABBBAAABBBAABAABAABBABABABABBAAABBBBBBBAABABBAABABBBAABBBBAAABAAAABABBABBBBBAABAABABAAAABBABABABAAAABBBAAAAABABBAABABABAAABBABAAAABABBBAAABABAABBAABBBAABAABBBAABABBBBABAAABAABBBBABBABAAABABBAABABBABBBBBBBABAABAAAABABABAAABABABABAABBAABBBABBBAAB...
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 0
Accepted
time: 2ms
memory: 16168kb
input:
500 1 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
250000
result:
ok 1 number(s): "250000"
Test #17:
score: 0
Accepted
time: 2ms
memory: 16216kb
input:
500 2 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
187500
result:
ok 1 number(s): "187500"
Test #18:
score: 0
Accepted
time: 126ms
memory: 16156kb
input:
500 499 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
125251
result:
ok 1 number(s): "125251"
Test #19:
score: 0
Accepted
time: 127ms
memory: 16216kb
input:
500 500 BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...
output:
125250
result:
ok 1 number(s): "125250"
Test #20:
score: -24
Wrong Answer
time: 3ms
memory: 16112kb
input:
500 10 ABAAABBBAABBABABAABAABABBBAABABABBAABABAABAABABABBBBABAABBAAAAAAABAABAABAABBABBABABBBABABBAABABBBBAABBBBABAAAAAAABAABAAABABABBAABABBABBAAAABAABBBAABABBABABBBBAAAABABBAAAAABAABBBBBBBBABBABBBAABBABBAAAABAABBBBBBAABBBAAAABABBAAABAABAABBBBAABBAAABBAABBAAAAABAAAAAABBAABABBABABBBABAAABBBAAABABAAABB...
output:
9218
result:
wrong answer 1st numbers differ - expected: '9129', found: '9218'
Subtask #3:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%