QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103681 | #6299. Binary String | MIT01# | WA | 224ms | 3612kb | C++17 | 3.5kb | 2023-05-07 07:47:09 | 2023-05-07 07:47:11 |
Judging History
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;
}
vi h;
int fl[maxn];
pair<int, vi> work(vi cur) {
int s = cur.size();
h.reserve(s);
int le = 0;
int t = 0;
for (int j = 0; j < s; j++) {
h.pb(j);
for (int m = 0; m < cur[j]; m++) {
while (h.size()) h.pop_back();
}
if (h.size()) chkmax(t, j - h[0] + 1);
}
// j ~ t + 1 ~ j
vi zrs(s);
for (int i = 0; i <= s; i++) fl[i] = 0;
h.clear();
h.reserve(s);
int tot = 0;
for (int j = 0; j < s; j++) {
h.pb(j);
fl[j] = 1;
for (int m = 0; m < cur[j]; m++) {
while (h.size()) {
fl[h.back()] = 0;
h.pop_back();
}
}
if (fl[j]) tot += 1;
if (j >= t && fl[j - t]) tot -= 1;
zrs[j] = tot;
}
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() {
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;
}
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 < per; u++) {
if (cur[u] != cur[u + per]) {
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);
}
}
/*
3
1
001001
0001111
*/
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3608kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 224ms
memory: 3612kb
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...
output:
1 18 18 3 18 2 3 4 18 3 2 3 3 3 4 5 18 18 3 3 2 2 3 4 3 19 3 3 4 4 5 6 18 18 18 3 3 2 3 4 2 18 2 3 3 3 4 5 3 19 19 3 3 3 3 4 4 20 4 4 5 5 6 7 18 6 18 3 18 2 3 4 3 3 2 3 3 3 4 5 2 18 18 3 2 2 3 4 3 19 3 3 4 4 5 6 3 19 19 3 19 3 3 4 3 19 3 3 3 3 4 5 4 20 20 4 4 4 4 4 5 21 5 5 6 6 7 8 18 18 6 3 18 2 3 ...
result:
wrong answer 4th numbers differ - expected: '19', found: '3'