QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#350362#6338. Choruszyz0716 3ms18048kbC++171.5kb2024-03-10 17:49:202024-03-10 17:49:20

Judging History

你现在查看的是最新测评结果

  • [2024-03-10 17:49:20]
  • 评测
  • 测评结果:16
  • 用时:3ms
  • 内存:18048kb
  • [2024-03-10 17:49:20]
  • 提交

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],len,cnta[N*2],cntb[N*2],mark[N*2],nxt[N*2];
ll cost[N*2],f[N*2],g[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);
		}
	}
	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;
	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;
	}
	fill(f,f+n*2+1,1e18);
	f[0]=0;
	For($,1,k){
		fill(g,g+n*2+1,1e18);
		For(i,1,n*2){
			if(!mark[i]) continue;
			For(j,0,i-1){
				if(mark[j]){
					g[i]=min(g[i],f[j]+cost[i]-cost[nxt[j]]-1LL*cntb[nxt[j]]*(cnta[i]-cnta[nxt[j]]));
				}
			}
		}
		copy(g,g+n*2+1,f);
	}
	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: 0ms
memory: 17928kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 3ms
memory: 17988kb

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 0ms
memory: 17972kb

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

score: 0
Accepted
time: 2ms
memory: 17988kb

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

score: 0
Accepted
time: 0ms
memory: 15932kb

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

score: 0
Accepted
time: 3ms
memory: 17936kb

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

score: 0
Accepted
time: 0ms
memory: 17988kb

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

score: 0
Accepted
time: 0ms
memory: 17988kb

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

score: 0
Accepted
time: 3ms
memory: 18048kb

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 0
Accepted
time: 0ms
memory: 17996kb

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

score: 0
Accepted
time: 3ms
memory: 17924kb

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

score: 0
Accepted
time: 0ms
memory: 18048kb

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: 0
Wrong Answer
time: 3ms
memory: 17924kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

1000000000000000041

result:

wrong answer 1st numbers differ - expected: '41', found: '1000000000000000041'

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%