QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#318635#6299. Binary StringNetwork_ErrorTL 31ms165672kbC++203.6kb2024-01-31 16:26:092024-01-31 16:26:10

Judging History

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

  • [2024-01-31 16:26:10]
  • 评测
  • 测评结果:TL
  • 用时:31ms
  • 内存:165672kb
  • [2024-01-31 16:26:09]
  • 提交

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 = 0;
		for (int i = 0; i < n; i++) {
			s[i] = (i ? s[i - 1] : 0) + (a[i] ? 1 : -1); if (s[i] < s[p]) p = i;
		}
		for (int i = 0; i < n; i++) b[i] = a[(p + i + 1) % n]; swap(a, b);
//		debb(a);
		/* ---------------------------- */
		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;
						if (r.l == r.r) break; 
					} else if (lenl > lenr) {
						r.r = 0; l.r -= lenr; 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();
		while (st.size()) {
			seg t = st.top(); st.pop();
			for (int i = t.l; i <= t.r; i++) b[i] = t.b;
		}
		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);
		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: 31ms
memory: 165672kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Time Limit Exceeded

input:

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

output:


result: