QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#103698#6299. Binary StringMIT01#WA 220ms7764kbC++173.2kb2023-05-07 08:35:532023-05-07 08:35:55

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-07 08:35:55]
  • 评测
  • 测评结果:WA
  • 用时:220ms
  • 内存:7764kb
  • [2023-05-07 08:35:53]
  • 提交

answer

#pragma GCC optimize("-Ofast","-ffast-math","-funroll-all-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<ll, ll>
#define db double
#define mp make_pair
#define pb push_back
#define fi first
#define vi vector<int>
#define se second
#define mod 998244353
const int maxn = 10000005;
char s[maxn];
int cur[maxn];

template <typename T> bool chkmin(T &a, T b) {
    return (b < a) ? (a = b, 1) : 0;
}

template <typename T> bool chkmax(T &a, T b) {
    return (b > a) ? (a = b, 1) : 0;
}
int fl[maxn];
pair<int, vi> work(vi cur) {
    int s = cur.size();
    
    vi h;
    h.clear();
    h.reserve(s);
    int le = 0;
    vi zrs(s);
    int t = 0;
    for (int j = 0; j < s; j++) {
        h.pb(j);
        for (int m = 0; m < cur[j]; m++) {
            if (h.size()) h.pop_back();
        }
        if (h.size()) chkmax(t, j - h[0] + 1);
        zrs[j] = h.size();
    }
    // j ~ t + 1 ~ j
    vi res(s);
    for (int j = 0; j < s; j++) {
        res[j] = cur[j] + zrs[j];
        if (j) res[j] -= zrs[j - 1];
    }
    return mp(t, res);
}
int solve() {
    int test = 0;
    scanf("%s", s);
    int n = strlen(s);
    int cnt[2] = {0, 0};
    for (int i = 0; i < n; i++)
        cnt[s[i] - '0'] += 1, 
        cur[i] = s[i] - '0';
    if (cnt[1] > cnt[0]) {
        for (int i = 0; i < n; i++)
            s[i] = '0' + '1' - s[i], 
            cur[i] ^= 1;
        swap(cnt[0], cnt[1]);
    }
    if (!cnt[1]) return 1;
    vi srt;
    for (int i = 0; i < n; i++) {
        if (cur[i]) {
            int len = 0;
            for (int j = i; j < i + n; j++) {
                if (cur[j % n]) {
                    if (j != i) {
                        srt.pb(len);
                        len = 0;
                    }
                }
                else len += 1;
            }
            srt.pb(len);
            break;
        }
    }
    
    auto period = [&](vi lens) {
        int id = 0;
        for (auto v : lens) {
            cur[id++] = 1;
            for (int j = 0; j < v; j++)
                cur[id++] = 0;
        }
        assert(id == n);
        for (int per = 1; per <= n; per++) {
            if (n % per != 0) continue;
            if (per == n) return n;
            int flag = 1;
            for (int u = 0; u < n; u++) {
                if (cur[u] != cur[(u + per) % n]) {
                    flag = 0;
                    break;
                }
            }
            if (flag) return per;
        }
        return 0;
    };
    int flag = 1;
    for (auto v : srt)
        if (v == 0) flag = 0;
    if (flag) return period(srt);

    int s = srt.size();
    int mxid = 0;
    int sum = 0;
    int mxsum = -1e9;
    for (int i = 0; i < s; i++) {
        sum += srt[i] - 1;
        if (chkmax(mxsum, sum)) mxid = i;
    }
    vi nw; nw.reserve(s);
    for (int j = mxid + 1; j < mxid + 1 + s; j++)
        nw.pb(srt[j % s]);
    auto res = work(nw);
    return res.fi + period(res.se);
}
int main() {
    int t; cin>>t;
    while (t--) {
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}
/*
3
1
001001
0001111
*/

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7764kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 220ms
memory: 7696kb

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 1536th numbers differ - expected: '25', found: '24'