QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132582#6338. Choruszhouhuanyi0 1ms18936kbC++232.3kb2023-07-30 17:23:312023-07-30 17:23:33

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-30 17:23:33]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:18936kb
  • [2023-07-30 17:23:31]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cmath>
#define N 2000000
#define inf 1e12
#define INF 2e18
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
struct reads
{
	int l,r,ps;
};
reads dque[N+1];
int n,k,top,lg[N+1],s[N+1],cnt[N+1],pst[N+1],pst2[N+1];
long long sx,S[N+1],S2[N+1],ds[N+1],dp[N+1];
char c[N+1];
long long F(int l,int r)
{
	if (l>r) return INF;
	int sl=(l-1)<<1,sr=r<<1,mid=(sl+sr)>>1;
	long long res=0;
	if (pst[sl]==-1||pst[sl]<sl||pst[sl]>mid)
	{
		if (s[sl]<=0) res+=S2[mid-sl]-(ds[mid]-ds[sl]);
	}
	else res+=(S2[mid-sl]-S2[pst[sl]-sl])-(ds[mid]-ds[pst[sl]]);
	if (pst2[sr]==-1||pst2[sr]<mid||pst2[sr]>sr)
	{
		if (s[sr]<=0) res+=S[sr-mid]-(ds[sr]-ds[mid]);
	}
	else res+=(S[sr-mid]-S[sr-pst2[sr]])-(ds[pst2[sr]]-ds[mid]);
	return (res>>1)+sx;
}
long long calc(long long x)
{
	int d;
	for (int i=1;i<=n;++i) dp[i]=INF;
	sx=x,dque[top=1]=(reads){1,n,0};
	for (int i=1;i<=n;++i)
	{
		d=0;
		for (int j=lg[top];j>=0;--j)
			if (d+(1<<j)<=top&&dque[d+(1<<j)].l<=i)
				d+=(1<<j);
		cnt[i]=cnt[dque[d].ps]+1,dp[i]=dp[dque[d].ps]+F(dque[d].ps+1,i);
		while (top&&dp[i]+F(i+1,dque[top].l)<dp[dque[top].ps]+F(dque[top].ps+1,dque[top].l)) top--;
		if (top&&dp[i]+F(i+1,dque[top].r)<dp[dque[top].ps]+F(dque[top].ps+1,dque[top].r))
		{
			d=dque[top].r;
			for (int j=lg[dque[top].r-dque[top].l];j>=0;--j)
				if (d-(1<<j)>=dque[top].l&&dp[i]+F(i+1,d-(1<<j))<dp[dque[top].ps]+F(dque[top].ps+1,d-(1<<j)))
					d-=(1<<j);
			dque[top].r=d-1;
		}
		d=dque[top].r+1;
		if (d<=n) dque[++top]=(reads){d,n,i};
	}
	return cnt[n];
}
int main()
{
	long long x=0;
	for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
	n=read(),k=read();
	for (int i=1;i<=(n<<1);++i)
	{
		cin>>c[i],S[i]=S[i-1]+i-1,S2[i]=S2[i-1]+i;
		if (c[i]=='A') s[i]=s[i-1]+1;
		else s[i]=s[i-1]-1;
		ds[i]=ds[i-1]+s[i];
	}
	for (int i=0;i<=(n<<1);++i) pst[i]=pst2[i]=-1;
	for (int i=0;i<=(n<<1);++i)
	{
		if (i-s[i]<=(n<<1)&&pst[i-s[i]]==-1) pst[i-s[i]]=i;
		if (i+s[i]<=(n<<1)&&pst2[i+s[i]]==-1) pst2[i+s[i]]=i;
	}
	for (int i=log(inf)/log(2);i>=0;--i)
		if (x+(1ll<<i)<=inf&&calc(x+(1ll<<i))>=k)
			x+=(1ll<<i);
	calc(x),printf("%lld\n",dp[n]-k*x);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

7 5
ABBAAABBABABBA

output:

2

result:

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

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%