QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318708#6299. Binary StringNetwork_ErrorTL 748ms14064kbC++203.9kb2024-01-31 17:35:012024-01-31 17:35:02

Judging History

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

  • [2024-01-31 17:35:02]
  • 评测
  • 测评结果:TL
  • 用时:748ms
  • 内存:14064kb
  • [2024-01-31 17:35:01]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int, int>
#define piii tuple<int, int, int>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define deb(var) cerr << #var << '=' << (var) << "; "
#define int long long
namespace Maths {
	const int mod = 998244353;
	int power(int x, int y) {
		int ans = 1; while (y) {
			if (y & 1) ans = 1ll * ans * x % mod; y >>= 1; x = 1ll * x * x % mod;
		} return ans;
	}
	int power(int x, int y, int mod) {
		int ans = 1; while (y) {
			if (y & 1) ans = 1ll * ans * x % mod; y >>= 1; x = 1ll * x * x % mod;
		} return ans;
	}
	int fac[1000010], inv[1000010];
	void init() {
		fac[0] = fac[1] = inv[0] = inv[1] = 1;
		for (int i = 2; i <= 1e6; i++) fac[i] = 1ll * fac[i - 1] * i % mod, inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
		for (int i = 2; i <= 1e6; i++) inv[i] = 1ll * inv[i] * inv[i - 1] % mod;
	}
	int binom(int n, int m) {
		return n < m || m < 0 ? 0 : 1ll * fac[n] * inv[n - m] % mod * inv[m] % mod;
	}
} using namespace Maths;
namespace Loser {
	int n, sum, s[10000010], b[10000010], a[10000010];
	struct seg {
		int l, r, b;
	}; stack<seg> st;
	int bor[10000010];
	int period(int* s) {
		for (int i = n; i >= 1; i--) s[i] = s[i - 1];
		bor[1] = 0;
		for (int i = 2; i <= n; i++) {
			bor[i] = bor[i - 1];
			while (bor[i] && s[bor[i] + 1] != s[i]) bor[i] = bor[bor[i]];
			if (s[bor[i] + 1] == s[i]) bor[i]++;
		}
//		cout<<"deb:"; for (int i=1;i<=n;i++)cout<<s[i];cout<<'\n';
		for (int x = bor[n]; x; x = bor[x]) {
			if (n % (n - x) == 0) return n - x;
		} return n;
	}
	char c[10000010];
	void debb(int*a){
//		cout<<"deb:";for(int i =0;i<n;i++)cout<<a[i];cout<<'\n';
	}
	void main() {
		cin >> c; n = strlen(c); sum = 0;
		for (int i = 0; i < n; i++) a[i] = c[i] ^ 48, sum += a[i];
		if (sum < n - sum) {
			for (int i = 0; i < n; i++) a[i] ^= 1; reverse(a, a + n), sum = n - sum;
		}
		int p = -1;
		for (int i = 0; i < n; i++) {
			s[i] = (i ? s[i - 1] : 0) + (a[i] ? 1 : -1); if ((p == -1 || s[i] < s[p]) && a[i] == 0) p = i;
		}
		for (int i = 0; i < n; i++) b[i] = a[(p + i + 1) % n]; 
		for (int i = 0; i < n; i++) swap(a[i], b[i]);
		debb(a);
		for (int i = 0; i < n; i++) {
			s[i] = (i ? s[i - 1] : 0) + (a[i] ? 1 : -1); assert(s[i]>=0);
		}
		/* ---------------------------- */
		int ans = 0;
		while (st.size()) st.pop();
		for (int i = 0, k; i < n; i++) {
			k = i; while (k + 1 < n && a[k + 1] == a[k]) ++k;
			if (k == i) continue;
			seg r = seg{i, k, a[k]}; i = k; 
			if (a[k]) {
				st.push(r);
			} else {
				while (st.size() && st.top().b) {
					seg l = st.top(); st.pop();
					int mid = (l.r + r.l) >> 1;
					int lenl = l.r - l.l + 1, lenr = r.r - r.l + 1;
					ans = max(ans, mid - l.r + min(lenl, lenr) - 1);
					if (lenl < lenr) {
						r.l += lenl - 1;
						if (r.l == r.r) break; 
					} else if (lenl > lenr) {
						r.r = 0; l.r -= lenr - 1; if (l.l != l.r) st.push(l); break;
					} else {
						r.r = 0; break;
					}
				}
				if (r.r > r.l) st.push(r);
			} 
		}
		/* ---------------------------- */
		for (int i = 0; i <= n; i++) b[i] = -1;
		bool flag = st.empty();
		int l=0,B;
		while (st.size()) {
			seg t = st.top(); st.pop();
//			deb(t.l),deb(t.r),deb(t.b)<<'\n';
			if(l)assert((l-t.r)%2==(t.b!=B)); l=t.l;B=t.b;
			for (int i = t.l; i <= t.r; i++) b[i] = t.b;
		}
//		debb(b); 
//		return;
		for (int i = 0; i < n; i++) {
			if (b[(i + 1) % n] == -1 && b[i] != -1) b[(i + 1) % n] = b[i] ^ 1; 
		}
		for (int i = 0; i < n; i++) {
			if (b[(i + 1) % n] == -1 && b[i] != -1) b[(i + 1) % n] = b[i] ^ 1; 
		}
		if (flag) {
			for (int i = 0; i < n; i++) b[i] = ~i & 1;
		}
		debb(b);deb(ans);
		cout << period(b) + ans << '\n';
	}
}
signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T = 1; cin >> T; while (T--) Loser::main(); return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: 0
Accepted
time: 351ms
memory: 13860kb

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:

ok 262144 numbers

Test #3:

score: 0
Accepted
time: 748ms
memory: 13800kb

input:

524288
0000000000000000000
1000000000000000000
0100000000000000000
1100000000000000000
0010000000000000000
1010000000000000000
0110000000000000000
1110000000000000000
0001000000000000000
1001000000000000000
0101000000000000000
1101000000000000000
0011000000000000000
1011000000000000000
0111000000000...

output:

1
19
19
20
19
19
20
21
19
19
19
21
20
20
21
22
19
19
19
20
19
19
21
22
20
20
20
22
21
21
22
23
19
19
19
20
19
19
20
22
19
19
19
22
21
21
22
23
20
20
20
20
20
20
22
23
21
21
21
23
22
22
23
24
19
19
19
20
19
19
20
21
19
19
19
21
20
20
22
23
19
19
19
20
19
19
22
23
21
21
21
23
22
22
23
24
20
20
20
20
2...

result:

ok 524288 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

952358
0011101111
101010101101
101111111010100
0101011000110001100
0101111101110
010
100100000111011
011010110110011
1010111
1
1111101010
11111011001111110
0101101101011
001
1100111100
100011
10
10
0001
011100
1100010
111111101010010
01001111110011011
01100
1010
10101111
01000111100011111110
10101
0...

output:

11
12
18
20
14
3
21
16
7
1
10
18
13
3
11
4
2
2
4
4
8
18
19
6
2
8
24
5
1
1
5
25
1
14
1
15
20
3
7
24
12
10
20
21
23
1
22
18
22
5
1
6
18
12
1
4
5
12
13
12
21
1
5
12
21
8
1
8
18
4
1
12
13
6
3
3
16
6
8
1
1
17
1
1
1
6
6
4
4
10
7
5
4
5
24
6
11
4
8
15
3
9
9
19
5
16
11
5
6
9
17
1
25
14
6
1
4
20
1
4
20
14
14
...

result: