QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411296#6299. Binary StringcrsfaaRE 1ms9916kbC++142.5kb2024-05-15 11:21:562024-05-15 11:21:56

Judging History

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

  • [2024-05-15 11:21:56]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:9916kb
  • [2024-05-15 11:21:56]
  • 提交

answer

#include<bits/stdc++.h>
#define Yukinoshita namespace
#define Yukino std
using Yukinoshita Yukino;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int mxn=1e7+5;
bool t[mxn],t2[mxn];
char s[mxn];
int pre[mxn];
struct seg
{
	int l,r;
	bool v;
	int len()
	{
		return r-l+1;
	}
};
int getp(int n,bool t[])
{
	int i,j;
	for(i=1;i<=n;i++)
		if(n%i==0)
		{
			for(j=i;j<n;j++)
				if(t[j]!=t[j-i])
					goto bre;
			return i;
			bre:;
		}
}
int main()
{
	int T=read();
	while(T--)
	{
		scanf("%s",s+1);
		int n=strlen(s+1),i,j;
		for(i=0;i<n;i++)
			t[i]=s[i+1]-'0';
		int sum=0;
		for(i=0;i<n;i++)
			sum+=t[i];
		if(sum<n-sum)
		{
			reverse(t,t+n);
			for(i=0;i<n;i++)
				t[i]^=1;
		}
		for(i=0;i<n;i++)
			if(!t[i])
				goto bre;
		puts("1");
		continue;
		bre:;
		vector<seg> a;
		for(i=0;i<n;i++)
			if(t[i]!=t[1])
			{
				for(j=0;j<n;j++)
					t2[j]=t[(j+i)%n];
				memcpy(t,t2,n);
				break;
			}
		for(i=0;i<n;i=j)
		{
			for(j=i;j<n&&t[j]==t[i];j++);
			if(j>i+1)
				a.push_back({i,j-1,t[i]});
		}
//		for(i=0;i<n;i++)
//			cout<<t[i];
//		puts("");
//		for(auto x:a)
//			cout<<x.l<<' '<<x.r<<' '<<x.v<<endl;
		vector<int> pre{0};
		for(auto x:a)
		{
			int v=(x.v?1:-1)*(x.r-x.l+1);
			pre.push_back(pre.back()+v);
		}
		pre.erase(pre.begin());
		int mn=2e9;
		for(i=0;i<pre.size();i++)
			mn=min(mn,pre[i]);
		for(i=0;i<pre.size();i++)
			if(pre[i]==mn)
			{
				vector<seg> tp;
				int lft=a[(i+1)%pre.size()].l;
				for(j=0;j<pre.size();j++)
				{
//					cout<<':'<<j<<endl;
					seg u=a[(i+1+j)%pre.size()];
					tp.push_back({(u.l-lft+n)%n,(u.r-lft+n)%n,u.v});
				}
				a=tp;
				break;
			}
//		for(auto x:a)
//			cout<<x.l<<' '<<x.r<<'#'<<x.v<<endl;
//		for(au)
		int mx=0;
		stack<seg> stk;
		for(auto x:a)
		{
			if(x.v) stk.push(x);
			else
			{
				int l=x.l,r=x.r,s=0;
				for(;;)
				{
					assert(stk.size());
					s+=(x.l-stk.top().len()-1)/2;
					x.r-=x.l-(stk.top().r+1);
					x.l=stk.top().r+1;
					if(stk.top().len()>=r-l+1)
					{
						mx=max(mx,s+r-l);
						break;
					}
					s+=stk.top().len()-1;
					r-=stk.top().len()-1;
					stk.pop();
				}
			}
		}
		memset(t,0,n);
		while(stk.size())
		{
			for(i=stk.top().l;i<=stk.top().r;i++)
				t[i]=1;
			stk.pop();
		}
		printf("%d\n",mx+getp(n,t));
	}
}
/*
1
001001
*/

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 9916kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Runtime Error

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
19
19
19
20
21
18
18
18
25
18
18
19
20
19
20
19
20
20
20
21
22
18
18
18
25
18
18
25
27
18
18
18
20
19
19
20
21
19
20
20
20
19
19
20
21
20
21
20
20
21
21
22
23
18
18
18
24
18
18
25
25
18
18
18
25
25
24
27
27
18
18
18
25
18
18
20
21
19
21
19
21
20
20
21
22
19
21
20
21
2...

result: