QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178714#6338. Choruskgqy#0 1ms8188kbC++141.7kb2023-09-14 11:27:392024-07-04 01:57:05

Judging History

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

  • [2024-07-04 01:57:05]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:8188kb
  • [2023-09-14 11:27:39]
  • 提交

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;
		int l=0,r=i-1;
		while(l<r){
			int mid=(l+r)>>1;
			if(dp[mid]+w[mid+1][i]+adv>dp[mid+1]+w[mid+2][i]+adv) l=mid+1;
			else r=mid;
		}
		dp[i]=dp[l]+w[l+1][i]+adv,cnt[i]=cnt[l]+1;
		// if(adv<=1)printf("%d %d\n",dp[i],cnt[i]);
	}
	return cnt[n];
}
main(){
	scanf("%lld%lld",&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=25000000;
	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("%lld\n",dp[n]-l*m+ans);
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 16
Accepted
time: 1ms
memory: 7892kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

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

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

score: -16
Wrong Answer
time: 0ms
memory: 8188kb

input:

10 2
AAABBABABBAAABBBAABB

output:

25000006

result:

wrong answer 1st numbers differ - expected: '11', found: '25000006'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%