QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#360775#6299. Binary Stringmazihang2022WA 122ms3664kbC++142.7kb2024-03-22 08:25:182024-03-22 08:25:18

Judging History

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

  • [2024-03-22 08:25:18]
  • 评测
  • 测评结果:WA
  • 用时:122ms
  • 内存:3664kb
  • [2024-03-22 08:25:18]
  • 提交

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);
		// for(int i=1;i<=n;i++) {
		// 	cerr<<a[i]<<" ";
		// }
		// cerr<<"\n";
		sum=0,pos=0;
		for(int i=1;i<=n;i++) {
			sum+=!a[i]?1:-1;
			if(!sum) {
				pos=i;
			}
		}
		rotate(a+1,a+pos+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) {
			bool ex=false;
			for(int i=1;i<=cnt;i++) {
				if(c[i]) {
					ex|=i==1;
					int j=i;
					while(j<=cnt&&c[j]) {
						j++;
					}
					j--;
					if(j==cnt&&ex) {

					} else {
						c[j]--;
					}
					c[i-1]++;
					i=j;
				}
			}
			// cerr<<"c: ";
			// for(int i=1;i<=n;i++) {
			// 	cerr<<c[i]<<" ";
			// }
			// cerr<<"\n";
			ans++;
		}
		// cerr<<ans<<"\n";
		int cur=0;
		for(int i=1;i<=cnt;i++) {
			res[++cur]=0;
			while(c[i]--) {
				res[++cur]=1;
			}
		}
		// 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: 1ms
memory: 3564kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 122ms
memory: 3664kb

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 152nd numbers differ - expected: '22', found: '21'