QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132586#6338. Choruszhouhuanyi16 5ms19780kbC++232.3kb2023-07-30 17:31:492023-07-30 17:31:51

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:31:51]
  • 评测
  • 测评结果:16
  • 用时:5ms
  • 内存:19780kb
  • [2023-07-30 17:31:49]
  • 提交

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,pst=0;
	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)
	{
		while (pst+1<=top&&dque[pst+1].l<=i) ++pst;
		cnt[i]=cnt[dque[pst].ps]+1,dp[i]=dp[dque[pst].ps]+F(dque[pst].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};
		if (cnt[i]>k) return n+1;
	}
	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: 16
Accepted

Test #1:

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

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 5ms
memory: 17912kb

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: 0
Accepted
time: 5ms
memory: 19468kb

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

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

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

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

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

100

result:

ok 1 number(s): "100"

Test #6:

score: 0
Accepted
time: 4ms
memory: 18056kb

input:

10 2
BBBBBBBBBBAAAAAAAAAA

output:

75

result:

ok 1 number(s): "75"

Test #7:

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

input:

10 9
BBBBBBBBBBAAAAAAAAAA

output:

56

result:

ok 1 number(s): "56"

Test #8:

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

input:

10 10
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

ok 1 number(s): "55"

Test #9:

score: 0
Accepted
time: 5ms
memory: 18372kb

input:

10 10
ABABABABABABABABABAB

output:

0

result:

ok 1 number(s): "0"

Test #10:

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

input:

10 2
ABAAABABABBBABABABAB

output:

14

result:

ok 1 number(s): "14"

Test #11:

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

input:

10 4
ABAABBAAABBBAAABBBAB

output:

2

result:

ok 1 number(s): "2"

Test #12:

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

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: 1ms
memory: 17760kb

input:

179 54
AAABABABABBAAABBABBABBABBBAAABAAAAABBBABAAAAABABBBAABBBABABBAABABAABABBBBABAABAABABABBBABBAABABBAABBAABABBAAABAAAAAAAABBAAAAABAAABBBBBBBABBAABBBABABAABBAABBABABABBABAAABABAAABABABBAABABAAABBABABABABABBAAABABBBBBBBAABBBAABABBBBABAABBAAAABAABBABABAABAAABABAAAABBBAABAAABBABABBBABAAABAABBBABBBBBA...

output:

2000000000000000000

result:

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

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%