QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#360584#6299. Binary Stringmazihang2022WA 119ms9976kbC++142.4kb2024-03-21 22:08:172024-03-21 22:08:18

Judging History

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

  • [2024-03-21 22:08:18]
  • 评测
  • 测评结果:WA
  • 用时:119ms
  • 内存:9976kb
  • [2024-03-21 22:08:17]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pii pair<int,int>
#define isz(v) (int)(v.size())
using namespace std;

const int maxn=1000005;
const int inf=0x3f3f3f3f;

namespace Solve {
	int n;
	int a[maxn];
	int c[maxn];
	int res[maxn];
	int fail[maxn];
	void clear() {
		fill(c+1,c+n+1,0);
		fill(res+1,res+n+1,0);
		fill(fail+1,fail+n+1,0);
	}
	void main(int tid) {
		string s;
		cin>>s;
		n=isz(s);
		for(int i=1;i<=n;i++) {
			a[i]=s[i-1]-'0';
		}
		int cc=accumulate(a+1,a+n+1,0);
		if(!cc||cc==n) {
			cout<<"1\n";
			return ;
		}
		if(cc<=n-cc) {
			for(int i=1;i<=n;i++) {
				a[i]^=1;
			}
			reverse(a+1,a+n+1);
		}
		int sum=0,mi=inf,pos=0;
		for(int i=1;i<=n;i++) {
			sum+=a[i]?1:-1;
			if(sum<mi) {
				mi=sum;
				pos=i;
			}
		}
		assert(pos);
		rotate(a+1,a+pos+1,a+n+1);
		if(a[n]==1) {
			int p=n;
			while(a[p]==1) {
				p--;
			}
			rotate(a+1,a+p+1,a+n+1);
		}
		// for(int i=1;i<=n;i++) {
		// 	cerr<<a[i]<<" ";
		// }
		// cerr<<"\n";
		int cnt=0,p=0;
		for(int i=1;i<=n;i++) {
			if(a[i]) {
				if(p) {
					++cnt;
					for(int j=p+1;j<=i;j++) {
						c[cnt]+=!a[j];
					}
				}
				p=i;
			}
		}
		++cnt;
		while(!a[n-c[cnt]]) {
			c[cnt]++;
		}
		// for(int i=1;i<=n;i++) {
		// 	cerr<<c[i]<<" ";
		// }
		// cerr<<"\n";
		int ans=0;
		while(*max_element(c+1,c+cnt+1)>1) {
			for(int i=1;i<=cnt;i++) {
				if(c[i]) {
					int j=i;
					while(j<=cnt&&c[j]) {
						j++;
					}
					j--;
					c[j]--;
					c[i-1]++;
					i=j;
				}
			}
			ans++;
		}
		int cur=0;
		for(int i=1;i<=cnt;i++) {
			res[++cur]=1;
			while(c[i]--) {
				res[++cur]=0;
			}
		}
		// for(int i=1;i<=n;i++) {
		// 	cerr<<res[i]<<" ";
		// }
		// cerr<<"\n";
		int j=0;
		for(int i=2;i<=n;i++) {
			while(j&&res[i]!=res[j+1]) {
				j=fail[j];
			}
			if(res[i]==res[j+1]) {
				j++;
			}
			fail[i]=j;
		}
		if(n%(n-fail[n])==0) {
			ans+=n-fail[n];
		} else {
			ans+=n;
		}
		cout<<ans<<"\n";
	}
	void init() {
		
	}
}

signed main() {
#ifndef ONLINE_JUDGE
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int T=1;
	cin>>T;
	Solve::init();
	for(int t=1;t<=T;t++) {
		Solve::main(t);
		Solve::clear();
	}
#ifndef ONLINE_JUDGE
	cerr<<"Time: "<<(1.0*clock()/CLOCKS_PER_SEC)*1000<<"ms\n";
#endif
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 119ms
memory: 9976kb

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
20
20
21
18
18
18
19
18
18
19
20
19
19
20
21
20
21
21
22
18
18
18
19
18
18
19
20
18
18
18
19
19
20
20
21
19
19
19
19
20
21
21
22
20
21
21
22
21
22
22
23
18
18
18
19
18
18
19
20
18
18
18
19
19
20
20
21
18
18
18
19
18
18
19
20
19
19
20
21
20
21
21
22
19
19
19
19
1...

result:

wrong answer 12th numbers differ - expected: '20', found: '19'