QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360058 | #6299. Binary String | ningago | WA | 128ms | 28528kb | C++14 | 5.6kb | 2024-03-21 10:53:38 | 2024-03-21 10:53:39 |
Judging History
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;
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];
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) { debug("???\n"); return; }
// printf("SS = %d, S = %d\n", SS, S);
// for(int i = 1; i <= n; i++) print(std::max(a[i], 0), 0);
// putc('\n');
std::rotate(a + 1, a + SS, a + 1 + n);
std::rotate(b + 1, b + S, b + 1 + m);
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);
for(int i = 1; i <= n; i++)
if(a[i] > 0 && a[i - 1] && !R[i])
c[(i - 1 + tim) % n + 1] = a[i];
if(!std::count(c + 1, c + 1 + n, 1))
c[1] = (tim & 1) ? -1 : 1;
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28528kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 128ms
memory: 28388kb
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'