QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#390269#6299. Binary StringXun_xiaoyaoWA 89ms3908kbC++142.1kb2024-04-15 11:05:092024-04-15 11:05:09

Judging History

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

  • [2024-04-15 11:05:09]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:3908kb
  • [2024-04-15 11:05:09]
  • 提交

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=top=0;fl=-1;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;
	}
	if(cnt1&&cnt0)
	{
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+(S[i]=='0'?-1:1);
			if(S[i]=='0'&&S[i<n?i+1:1]=='1'&&(fl==-1||sum[i]<sum[fl])) fl=i;	
		}
		fl=(fl<n?fl+1:1);
		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;i++) printf("%c",S[i]);printf("\n");

	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(cnt)
				{
					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;i<=top;i++) printf("(%d %d)\n",stk[top].first,stk[top].second);

	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=1;i<=n;i++) printf("%c",S[i]);printf("\n");
	
	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)+(nxt[n]*2>=n?n-nxt[n]:n));
}
int main()
{
	int T=Qread();
	while(T--) solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3828kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 89ms
memory: 3908kb

input:

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

output:

1
18
18
19
18
18
19
20
18
18
18
20
19
19
20
21
18
18
18
19
18
18
20
21
19
19
19
21
20
20
21
22
18
18
18
19
18
18
19
21
18
18
18
21
20
20
21
22
19
19
19
19
19
19
21
22
20
20
20
22
21
21
22
23
18
18
18
19
18
18
19
20
18
18
18
20
19
19
21
22
18
18
18
19
18
18
21
22
20
20
20
22
21
21
22
23
19
19
19
19
1...

result:

wrong answer 514th numbers differ - expected: '9', found: '18'