QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#359088#6299. Binary StringJCY_WA 78ms9820kbC++142.7kb2024-03-20 12:37:182024-03-20 12:37:18

Judging History

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

  • [2024-03-20 12:37:18]
  • 评测
  • 测评结果:WA
  • 用时:78ms
  • 内存:9820kb
  • [2024-03-20 12:37:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using i128 = __int128;
using u128 = unsigned __int128;
template <typename T>
void chkmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <typename T>
void chkmin(T &x, const T &y) {
  if (y < x) x = y;
}
constexpr int MAXN = 1e7 + 10;
int n, tp, nxt[MAXN];
char a[MAXN], b[MAXN];
pair<int, int> stk[MAXN];
int add(int x, int y) {
  x += y;
  return x >= n ? x - n : x;
}
void inc(int &x, int y) {
  x += y;
  if (x >= n) x -= n;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int cas;
  cin >> cas;
  for (int turn = 1; turn <= cas; ++turn) {
    cin >> a;
    if (turn == 512) cout << a << "\n";
    n = strlen(a);
    if (count(a, a + n, '1') * 2 < n) {
      reverse(a, a + n);
      for (int i = 0; i < n; ++i) a[i] ^= 1;
    }
    int pos = 0;
    for (int i = 0, sum = 0, mini = 0; i < n; ++i) {
      if (sum < mini) {
        mini = sum;
        pos = i;
      }
      sum += (a[i] == '1' ? 1 : -1);
    }
    rotate(a, a + pos, a + n);
    tp = 0;
    int ans = 0;
    for (int l = 0, r; l < n; l = r) {
      r = l + 1;
      while (r < n && a[r] == a[l]) ++r;
      if (r - l == 1) continue;
      if (a[l] == '1') {
        stk[++tp] = {l, r - l - 1};
      } else {
        int rem = r - l - 1;
        while (rem) {
          int red = min(rem, stk[tp].second);
          chkmax(ans, (r - stk[tp].first - (max(rem, stk[tp].second) - rem + 2)) / 2);
          rem -= red;
          stk[tp].second -= red;
          if (!stk[tp].second) --tp;
        }
      }
    }
    fill(b, b + n, '0');
    auto cover = [&](int l, int r) {
      l %= n;
      r %= n;
      if (l <= r) {
        fill(b + l, b + r + 1, '1');
      } else {
        fill(b + l, b + n, '1');
        fill(b, b + r + 1, '1');
      }
    };
    if (!tp) {
      for (int i = 0; i < n; i += 2) b[i] = '1';
    } else {
      for (; tp; --tp) cover(stk[tp].first + ans, stk[tp].first + ans + stk[tp].second);
      for (int i = 0; i < n; ++i) {
        if (b[i] == '0' || b[add(i, 1)] == '1') continue;
        for (int j = add(i, 2); b[j] != '1'; inc(j, 2)) b[j] = '1';
      }
    }
    nxt[0] = -1;
    for (int i = 1; i < n; ++i) {
      int p = nxt[i - 1];
      while (p != -1 && b[p + 1] != b[i]) p = nxt[p];
      nxt[i] = (b[p + 1] == b[i] ? p + 1 : -1);
    }
    ans += (n % (n - nxt[n - 1] - 1) ? n : n - nxt[n - 1] - 1);
    cout << ans << "\n";
  }
  return 0;
}
/*
g++ B.cpp -o B -std=c++14 -O2 -Wall -Wextra -Wshadow -g -fsanitize=address,undefined
*/
/*
1
111111111000000000
*/

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 9808kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 78ms
memory: 9820kb

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