QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178642#6338. Choruskgqy#0 1ms8204kbC++141.5kb2023-09-14 10:18:102024-07-04 01:56:56

Judging History

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

  • [2024-07-04 01:56:56]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:8204kb
  • [2023-09-14 10:18:10]
  • 提交

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--;
	}
	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*cnt[n]+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: 8204kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -16
Wrong Answer
time: 1ms
memory: 8008kb

input:

7 5
ABBAAABBABABBA

output:

4

result:

wrong answer 1st numbers differ - expected: '3', found: '4'

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%