QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178645#6338. Choruskgqy#16 10ms12972kbC++141.6kb2023-09-14 10:21:532024-07-04 01:57:00

Judging History

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

  • [2024-07-04 01:57:00]
  • 评测
  • 测评结果:16
  • 用时:10ms
  • 内存:12972kb
  • [2023-09-14 10:21:53]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
	int w=0;char ch=getchar();
	while(!isdigit(ch)) ch=getchar();
	while(isdigit(ch)) w=w*10+ch-'0',ch=getchar();
	return w; 
}
int n,m;
char ch[2000005];
int bel[2000005];
queue<int> q;
int ans;
int dp[5005],cnt[5005],w[5005][5005];
int check(int adv){
	// if(adv<=1)printf("adv %d\n",adv);
	for(int i=1;i<=n;i++){
		dp[i]=n*n*4;
		for(int j=0;j<i;j++){
			if(dp[j]+w[j+1][i]+adv<dp[i]) dp[i]=dp[j]+w[j+1][i]+adv,cnt[i]=cnt[j]+1;
		}
		// if(adv<=1)printf("%d %d\n",dp[i],cnt[i]);
	}
	return cnt[n];
}
main(){
	scanf("%d%d",&n,&m);
	scanf("%s",ch+1);
	for(int i=1,sum=0;i<=n*2;i++){
		if(ch[i]=='A'){
			if(sum<0){
				int fs=i+sum;
				ch[fs]='A';
				ch[i]='B';
				ans-=sum;
			}
			sum++;
		}
		else sum--;
	}
	// printf("ans %d\n",ans);
	// for(int i=1;i<=n*2;i++) putchar(ch[i]);puts("");
	for(int i=1;i<=n*2;i++){
		if(ch[i]=='A') q.push(i);
		else bel[i]=q.front(),q.pop();
		// printf("%d ",bel[i]);
	}
	// puts("");
	for(int i=1,id=1;i<=n;i++,id++){
		while(ch[id]=='B') id++;
		// printf("id %d %d\n",i,id);
		for(int j=i,jid=id,sumb=0;j<=n;j++,jid++){
			while(ch[jid]=='B') sumb+=(bel[jid]>=id),jid++;
			w[i][j]=w[i][j-1]+sumb;
			// printf("w %d %d %d %d\n",i,j,w[i][j],jid);
		}
	}
	// for(int i=1;i<=n;i++){
	// 	for(int j=i;j<=n;j++){
	// 		printf("%d %d %d\n",i,j,w[i][j]);
	// 	}
	// }
	int l=0,r=1000000;
	while(l<r){
		int mid=(l+r)>>1;
		if(check(mid)<=m) r=mid;
		else l=mid+1;
	}
	check(l);
	// printf("%d\n",l);
	printf("%d\n",dp[n]-l*m+ans);
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 16
Accepted

Test #1:

score: 16
Accepted
time: 0ms
memory: 7852kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

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

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

score: 0
Accepted
time: 1ms
memory: 8236kb

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

score: 0
Accepted
time: 1ms
memory: 7924kb

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

score: 0
Accepted
time: 1ms
memory: 8028kb

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

score: 0
Accepted
time: 1ms
memory: 8024kb

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

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

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

score: 0
Accepted
time: 1ms
memory: 7944kb

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

score: 0
Accepted
time: 1ms
memory: 7940kb

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

score: 0
Accepted
time: 1ms
memory: 7920kb

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

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

input:

10 4
ABAAABBBAAABBBAABBAB

output:

2

result:

ok 1 number(s): "2"

Subtask #2:

score: 0
Runtime Error

Dependency #1:

100%
Accepted

Test #13:

score: 24
Accepted
time: 0ms
memory: 9004kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

41

result:

ok 1 number(s): "41"

Test #14:

score: 0
Accepted
time: 10ms
memory: 12596kb

input:

500 93
ABABAABBBBABAABAABAAAAABABBBBBABABBABAAAABAABBBBABAABBBAAABBABABAAAABAABBABAAABBAAABBBABBAAAAAABABABBBABABBBAABAAABAABABAAABAAABBAAABABBBAABBABBBABBBAABAAAABBBAABBBABAAAAAABABABBBABAAAABBBBBAABBBABAAABAABBBABABAABABBBBABBABBBBBBBABAAAABAABAABBABBBAABBBAAAAAAABBAABAAABBBABBBAAABABBBBAABBBABABB...

output:

235

result:

ok 1 number(s): "235"

Test #15:

score: 0
Accepted
time: 10ms
memory: 12540kb

input:

500 273
AAABBABAABAAABABAABBBABAAAABBAAABBABAAABBABABAAAABBBAAABBBAABAABAABBABABABABBAAABBBBBBBAABABBAABABBBAABBBBAAABAAAABABBABBBBBAABAABABAAAABBABABABAAAABBBAAAAABABBAABABABAAABBABAAAABABBBAAABABAABBAABBBAABAABBBAABABBBBABAAABAABBBBABBABAAABABBAABABBABBBBBBBABAABAAAABABABAAABABABABAABBAABBBABBBAAB...

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 6ms
memory: 12864kb

input:

500 1
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

250000

result:

ok 1 number(s): "250000"

Test #17:

score: 0
Accepted
time: 6ms
memory: 12500kb

input:

500 2
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

187500

result:

ok 1 number(s): "187500"

Test #18:

score: 0
Accepted
time: 10ms
memory: 12812kb

input:

500 499
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

125251

result:

ok 1 number(s): "125251"

Test #19:

score: 0
Accepted
time: 10ms
memory: 10492kb

input:

500 500
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB...

output:

125250

result:

ok 1 number(s): "125250"

Test #20:

score: 0
Accepted
time: 6ms
memory: 10824kb

input:

500 10
ABAAABBBAABBABABAABAABABBBAABABABBAABABAABAABABABBBBABAABBAAAAAAABAABAABAABBABBABABBBABABBAABABBBBAABBBBABAAAAAAABAABAAABABABBAABABBABBAAAABAABBBAABABBABABBBBAAAABABBAAAAABAABBBBBBBBABBABBBAABBABBAAAABAABBBBBBAABBBAAAABABBAAABAABAABBBBAABBAAABBAABBAAAAABAAAAAABBAABABBABABBBABAAABBBAAABABAAABB...

output:

9129

result:

ok 1 number(s): "9129"

Test #21:

score: 0
Accepted
time: 10ms
memory: 12972kb

input:

500 500
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

0

result:

ok 1 number(s): "0"

Test #22:

score: 0
Accepted
time: 10ms
memory: 12700kb

input:

500 416
ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...

output:

84

result:

ok 1 number(s): "84"

Test #23:

score: -24
Runtime Error

input:

499 167
ABAABBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAABBBAAAB...

output:


result:


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%