QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#390251#6299. Binary StringXun_xiaoyaoWA 999ms9956kbC++141.8kb2024-04-15 10:49:352024-04-15 10:49:35

Judging History

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

  • [2024-04-15 10:49:35]
  • 评测
  • 测评结果:WA
  • 用时:999ms
  • 内存:9956kb
  • [2024-04-15 10:49:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int Qread()
{
	int x=0;char ch=getchar();
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=getchar();
	return x;
}
int get_str(char *S)
{
	int len=1;
	do S[1]=getchar();while(S[1]!='0'&&S[1]!='1');
	do S[++len]=getchar();while(S[len]=='0'||S[len]=='1');
	return len-1;
}
int n;char S[20000010];
int sum[10000010];
int cnt1,cnt0,fl;
pair<int,int> stk[10000010];
int top,typ,cnt,sta,tim;
int nxt[10000010];
void solve()
{
	cnt1=cnt0=fl=top=0;tim=-2;
	n=get_str(S);
	for(int i=1;i<=n;i++)
		if(S[i]=='0') cnt0++;else cnt1++;
	if(cnt1<cnt0)
	{
		reverse(S+1,S+n+1);
		swap(cnt1,cnt0);
		for(int i=1;i<=n;i++) S[i]^=1;
	}
	for(int i=1;i<=n;i++)
	{
		sum[i]=sum[i-1]+(S[i]=='0'?-1:1);
		if(sum[i]<sum[fl]) fl=i;	
	}
	fl++;
	for(int i=1;i<fl;i++) S[n+i]=S[i];
	for(int i=1;i<=n;i++) S[i]=S[fl+i-1];
	for(int i=1;i<=n;)
	{
		sta=i,cnt=0,typ=(S[i]=='1');
		while(i<=n&&S[sta]==S[i]) cnt++,i++;
		cnt-=1;
		if(cnt)
		{
			if(typ) stk[++top]=make_pair(cnt,sta);
			else
			{
				while(1)
				{
					if(stk[top].first<=cnt)
					{
						cnt-=stk[top].first;
						tim=max(tim,(i-1-cnt)-(stk[top].second+1)-1);
						top--;
					}
					else
					{
						stk[top].first-=cnt;
						tim=max(tim,(i-1)-(stk[top].second+stk[top].first+1)-1);
						break;
					}
				}
			}
		}
	}
	for(int i=1,fl=1,typ=1;i<=n;i++,typ^=1)
	{
		if(fl<=top&&stk[top].second==i)
		{
			while(stk[top].first--)
				S[i++]='1';
		}
		S[i]=(typ?'1':'0');
	}
	
	for(int i=2;i<=n;i++)
	{
		int p=nxt[i-1];
		while(p&&S[p+1]!=S[i]) p=nxt[p];
		if(S[p+1]==S[i]) nxt[i]=p+1;
		else nxt[i]=0;
	}
	printf("%d\n",(1+tim/2)+(n-nxt[n]));
}
int main()
{
	int T=Qread();
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9940kb

input:

3
1
001001
0001111

output:

1
3
9

result:

ok 3 number(s): "1 3 9"

Test #2:

score: -100
Wrong Answer
time: 999ms
memory: 9956kb

input:

262144
000000000000000000
100000000000000000
010000000000000000
110000000000000000
001000000000000000
101000000000000000
011000000000000000
111000000000000000
000100000000000000
100100000000000000
010100000000000000
110100000000000000
001100000000000000
101100000000000000
011100000000000000
11110000...

output:

1
18
17
19
17
18
18
20
16
15
17
20
18
19
19
21
17
16
15
20
17
18
19
21
17
16
18
21
19
20
20
22
16
15
14
14
17
13
19
21
16
15
17
21
19
20
20
22
18
17
16
21
18
19
20
22
18
17
19
22
20
21
21
23
17
16
15
15
17
14
14
21
16
15
13
21
19
20
20
22
17
16
15
21
17
18
20
22
18
17
19
22
20
21
21
23
17
16
15
14
1...

result:

wrong answer 3rd numbers differ - expected: '18', found: '17'