QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#360074#6299. Binary StringningagoWA 132ms28436kbC++145.8kb2024-03-21 11:13:592024-03-21 11:14:01

Judging History

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

  • [2024-03-21 11:14:01]
  • 评测
  • 测评结果:WA
  • 用时:132ms
  • 内存:28436kb
  • [2024-03-21 11:13:59]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <cctype>
#include <set>
#include <random>

namespace uvu
{
#define LOCAL ____________DONT_DEFINE_ME____________
#define ll long long
#define inf 0x3f3f3f3f
// #define int long long
// #define inf 0x3f3f3f3f3f3f3f3fll
#define intll 0x3f3f3f3f3f3f3f3fll
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define gline debug("now is #%d\n", __LINE__)
#define pii std::pair <int, int>
#define mkp std::make_pair
#define fi first
#define se second
char _ST_;
const int BUFSIZE = (1 << 20);
char ibuf[BUFSIZE], *iS = ibuf, *iT = ibuf;
char obuf[BUFSIZE], *oS = obuf, *oT = obuf + BUFSIZE;
char getc()
{
#ifdef LOCAL
	return getchar();
#else
	if(iS == iT) iT = (iS = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
	return iS == iT ? EOF : *iS++;
#endif
#define getchar ERR
}

void Flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; }
struct Flusher { ~Flusher(){ Flush(); } }iamflusher;

void putc(char c)
{
#ifdef LOCAL
	putchar(c);
#else
	*oS++ = c;
	if(oS == oT) Flush();
#endif
#define putchar ERR
}

template <typename T = int> T read()
{
	T x = 0, f = 1; char c = getc();
	for(; !isdigit(c); c = getc()) if(c == '-') f = -1;
	for(;  isdigit(c); c = getc()) x = (x << 3) + (x << 1) + (c ^ 48);
	return x * f;
}

template <typename T> void print(T x, char c)
{
static int sta[BUFSIZE], top;
	top = 0;
	if(x < 0) putc('-'), x = -x;
	if(!x) sta[top = 1] = 0;
	for(; x; x /= 10) sta[++top] = x % 10;
	for(; top; ) putc(sta[top--] ^ 48);
	if(c) putc(c);
}

int readstr(char *s, int base)
{
	int idx = base - 1; char c = getc();
	for(; !(isdigit(c) || isalpha(c) || c == '#' || c == '.'); c = getc());
	for(;   isdigit(c) || isalpha(c) || c == '#' || c == '.' ; c = getc()) s[++idx] = c;
	return idx - base + 1;
}

void printf(const char *s) { for(; *s; s++) putc(*s); }
template <typename T, typename ... Args>
void printf(const char *s, T x, Args ... rest)
{
	for(; *s; s++)
	{
		if(*s != '%') { putc(*s); continue; }
		s++; if(*s == 'd') print(x, 0);
		else if(*s == 'c') putc(x);
		printf(s + 1, rest ...);
		return;
	}
}

template <typename T> void ckmax(T &x, T y) { x = x > y ? x : y; }
template <typename T> void ckmin(T &x, T y) { x = x < y ? x : y; }
#define mod 998244353
// #define mod 1000000007
int sm(int x) { return x >= mod ? x - mod : x; }
void plus_(int &x, int y) { x = sm(x + y); }
void mul_(int &x, int y) { x = 1ll * x * y % mod; }
int ksm(int a, int b) { int res = 1; for(; b; b >>= 1, mul_(a, a)) if(b & 1) mul_(res, a); return res; }

#define N 10000010
int n, m; char str[N];
int a[N], suf[N], pre[N], sum[N];
int L[N], R[N];
pii b[N], sta[N]; int top;
int c[N], fail[N];

void solve()
{
	// memset(h, idx = -1, sizeof(h));
	n = readstr(str, 1);
	for(int i = 1; i <= n; i++) a[i] = (str[i] == '1'), R[i] = c[i] = fail[i] = 0;
	if(std::count(a + 1, a + 1 + n, 0) >= std::count(a + 1, a + 1 + n, 1))
	{
		for(int i = 1; i <= n; i++) a[i] ^= 1;
		std::reverse(a + 1, a + 1 + n);
	}
	if(std::count(a + 1, a + 1 + n, 1) == n) { print(1, '\n'); return; }
	for(int i = 1; i <= n; i++) if(!a[i]) a[i]--;
	// for(int i = 1; i <= n; i++) print(std::max(a[i], 0), 0);
	// putc('\n');
	m = 0;
	a[0] = a[n], a[n + 1] = a[1];
	bool flag = 1;
	for(int i = 1; i <= n; i++) flag &= (a[i] != a[i - 1]);
	if(flag) 
	{
		print(2, '\n');
		return;
	}
	for(int i = 1; i <= n; i++) if((a[i] == 1 && a[i - 1] == 1) || (a[i] == -1 && a[i + 1] == -1)) 
		b[++m] = mkp(a[i], i);
	pre[0] = suf[m + 1] = sum[m + 1] = 0;
	for(int i = 1; i <= m; i++) pre[i] = pre[i - 1] + b[i].fi;
	for(int i = 1; i <= m; i++) ckmin(pre[i], pre[i - 1]);
	for(int i = m; i >= 1; i--) suf[i] = std::min(suf[i + 1] + b[i].fi, b[i].fi);
	for(int i = m; i >= 1; i--) sum[i] = sum[i + 1] + b[i].fi;
	int S = 0, SS = b[S].se;
	// for(int i = 1; i <= m; i++) print(std::max(b[i].fi, 0), 0);
	// putc('\n');
	for(int i = 1; i <= m; i++) if(suf[i] >= 0 && sum[i] + pre[i - 1] >= 0) S = i;
	SS = b[S].se;
	if(!S) { return; }
	// printf("SS = %d, S = %d\n", SS, S);
	std::rotate(a + 1, a + SS, a + 1 + n);
	std::rotate(b + 1, b + S, b + 1 + m);
	// for(int i = 1; i <= n; i++) print(std::max(a[i], 0), 0);
	// putc('\n');
	top = 0;
	for(int i = 1; i <= m; i++)
	{
		if(b[i].se >= SS) b[i].se = b[i].se - SS + 1;
		else b[i].se = b[i].se - SS + 1 + n;
		if(b[i].fi > 0) sta[++top] = b[i];
		else R[sta[top].se] = b[i].se, L[b[i].se] = sta[top--].se;
	}
	// for(int i = 1; i <= n; i++) print(std::max(a[i], 0), 0);
	// putc('\n');
	int tim = 0;
	for(int i = 1; i <= n; i++)
		if(R[i]) ckmax(tim, (R[i] - i + 1) / 2);
	// for(int i = 1; i <= n; i++) printf("R[%d] = %d\n", i, R[i]);
	// printf("tim = %d\n", tim);
	a[0] = a[n];
	for(int i = 1; i <= n; i++)
		if(a[i] > 0 && a[i - 1] && !R[i]) 
			c[(i - 1 + tim) % n + 1] = a[i];
	// for(int i = 1; i <= n; i++) print(c[i], 0);
	// putc('\n');
	for(int i = 1; i < n; i++) if(c[i] && !c[i + 1]) c[i + 1] = -c[i];
	for(int i = n; i > 1; i--) if(c[i] && !c[i - 1]) c[i - 1] = -c[i];
	// for(int i = 1; i <= n; i++) print(std::max(c[i], 0), 0);
	// putc('\n');
	int ans = tim, t = n;
	for(int i = 2, p = 0; i <= n; i++)
	{
		for(; p && c[p + 1] != c[i]; p = fail[p]);
		if(c[p + 1] == c[i]) p++;
		fail[i] = p;
	}
	// printf("fail[%d] = %d\n", n, fail[n]);
	if(fail[n] && n % (n - fail[n]) == 0) t = n - fail[n];
	ans += t;
	print(ans, '\n');
}
void init()
{

}
char _ED_;
void mian()
{
	debug("%.3f MB\n", abs(&_ST_ - &_ED_) / 1024.0 / 1024);
	init();
	for(int T = read(); T; solve(), T--);
}

#ifdef int
	#undef int
#endif
}

int main()
{
	// freopen("tmp.in", "r", stdin);
	// freopen("tmp.out", "w", stdout);
	uvu::mian(); return 0;
}

詳細信息

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 132ms
memory: 28344kb

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 768th numbers differ - expected: '10', found: '26'