QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#381538 | #6299. Binary String | Talaodi | TL | 1176ms | 3896kb | C++23 | 6.5kb | 2024-04-07 18:41:34 | 2024-04-07 18:41:34 |
Judging History
answer
#include <bits/stdc++.h>
namespace IO {
template <class Stream>
void write_int128(Stream& out, __int128 v) {
if (v >= 10) {
write_int128(out, v / 10);
}
out << (int) (v % 10);
}
template <class Stream>
Stream& fmtbase(Stream& out, const char* format) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
throw std::invalid_argument("Error Number of Parameters!");
}
out << *format;
}
return out;
}
template <class Stream, class... Nxt>
Stream& fmtbase(Stream& out, const char* format, const __int128& value, const Nxt&... nxt) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
write_int128(out, value);
return fmtbase(out, format + 2, nxt...);
}
out << *format;
}
throw std::invalid_argument("Error Number of Parameters!");
}
template <class Stream, class Fst, class... Nxt>
Stream& fmtbase(Stream& out, const char* format, const Fst& value, const Nxt&... nxt) {
for (; *format; format++) {
if (*format == '{' && *(format + 1) == '}') {
out << value;
return fmtbase(out, format + 2, nxt...);
}
out << *format;
}
throw std::invalid_argument("Error Number of Parameters!");
}
template <class... Ps>
std::string to_string(const char* format, const Ps&... ps) {
std::stringstream ss;
fmtbase(ss, format, ps...);
return ss.str();
}
template <class... Ps>
std::ostream& fmtout(const char* format, const Ps&... ps) {
return fmtbase(std::cout, format, ps...);
}
template <class... Ps>
std::ostream& fmterr(const char* format, const Ps&... ps) {
return fmtbase(std::cerr, format, ps...);
}
std::istream& allin() {
return std::cin;
}
template <class Fst, class ... Nxt>
std::istream& allin(Fst& fst, Nxt&... nxt) {
std::cin >> fst;
return allin(nxt...);
}
template <class Iter>
std::istream& rangin(Iter begin, Iter end) {
while (begin != end) {
std::cin >> *begin;
begin++;
}
return std::cin;
}
namespace Fast {
bool sync = false;
char buf[1 << 23];
char *p1 = buf, *p2 = buf;
void sync_with_ios(bool s) {
sync = s;
}
inline char getchar() {
if (sync) {
return (char) std::cin.get();
}
else {
return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 22, stdin)), p1 == p2 ? EOF : *p1++;
}
}
void read() { }
template <class T, class... U>
void read(T& x, U&... us) {
x = 0;
T pos = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') {
pos = -1;
}
c = getchar();
}
while (isdigit(c)) {
x = 10 * x + c - '0';
c = getchar();
}
x *= pos;
read(us...);
}
template <class T>
void write(const T& t) {
if (t > 10) {
write(t / 10);
}
std::cout << (int) (t % 10);
}
}
}
namespace Solve {
using namespace IO;
using ll = long long;
using ul = unsigned long long;
using db = double;
using ld = long double;
using ui = unsigned;
using ib = __int128;
using ub = __uint128_t;
int const INF = std::numeric_limits<int>::max();
int const NINF = std::numeric_limits<int>::min();
ll const LINF = std::numeric_limits<ll>::max();
ll const LNINF = std::numeric_limits<ll>::min();
ld const EPS = 1e-8;
std::mt19937_64 mt;
ll rnd(ll l, ll r) {
return std::uniform_int_distribution<ll>(l, r)(mt);
}
template <class T>
inline int isz(const T& v) {
return v.size();
}
template <class T>
inline T& ckmx(T& a, T b) {
return a < b ? (a = b) : a;
}
template <class T>
inline T& ckmi(T& a, T b) {
return b < a ? (a = b) : a;
}
int const N = 1e7 + 10;
int cc[2];
int same[N + 1];
int s[N + 1];
int vis[N + 1];
int nxt[N + 1];
int n;
void read() {
std::string t;
std::cin >> t;
n = isz(t);
for (int i = 0; i < n; i++) {
s[i] = t[i] - '0';
}
}
void main() {
read();
for (int i = 0; i < n; i++) {
same[i] = s[i] == s[(i + 1) % n];
cc[s[i]] += same[i];
}
if (cc[0] == n || cc[1] == n) {
fmtout("1\n");
return;
}
std::vector<int> pos;
for (int i = 0; i < n; i++) {
if (s[i] == 0 && s[(i + 1) % n] == 1) {
pos.push_back(i);
}
}
auto prev = [&](int x) {
return (x + n - 1) % n;
};
auto next = [&](int x) {
return (x + 1) % n;
};
auto upd = [&](int x) {
cc[s[x]] -= same[x];
cc[s[prev(x)]] -= same[prev(x)];
s[x] ^= 1;
same[x] = s[x] == s[(x + 1) % n];
same[prev(x)] = s[prev(x)] == s[x];
cc[s[x]] += same[x];
cc[s[prev(x)]] += same[prev(x)];
};
int ans = 0;
while (cc[0] && cc[1]) {
for (auto& v : pos) {
upd(v);
upd(next(v));
}
std::vector<int> npos;
auto chk = [&](int x) {
if (vis[x]) {
return;
}
if (s[x] == 0 && s[next(x)] == 1) {
npos.push_back(x);
vis[x] = 1;
}
};
for (auto& v : pos) {
chk(next(v));
chk(prev(v));
}
pos = npos;
for (auto& v : pos) {
vis[v] = 0;
}
ans++;
}
// for (int i = 0; i < n; i++) {
// fmterr("{}", s[i]);
// }
// fmterr("\n");
// fmterr("? {}\n", ans);
int j = -1;
nxt[0] = -1;
for (int i = 1; i < n; i++) {
while (j != -1 && s[j + 1] != s[i]) {
j = nxt[j];
}
if (s[j + 1] == s[i]) {
j++;
}
nxt[i] = j;
}
// for (int i = 0; i < n; i++) {
// fmterr("{} ", nxt[i]);
// }
// fmterr("\n");
int len = nxt[n - 1] + 1;
// fmterr("? {}\n", len);
if (len >= (n + 1) / 2 && n % (n - len) == 0) {
ans += n - len;
}
else {
ans += n;
}
fmtout("{}\n", ans);
}
void init() {
}
void clear() {
for (int i = 0; i <= n; i++) {
same[i] = vis[i] = s[i] = nxt[i] = 0;
}
cc[0] = cc[1] = 0;
}
}
signed main() {
#ifndef ONLINE_JUDGE
auto input_file = freopen("d.in", "r", stdin);
auto output_file = freopen("d.out", "w", stdout);
#endif
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
IO::fmterr("seed: {}\n", seed);
Solve::mt.seed(seed);
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
std::cin >> t;
Solve::init();
for (int id = 1; id <= t; id++) {
Solve::main();
Solve::clear();
}
#ifndef ONLINE_JUDGE
std::cout.flush();
fclose(input_file);
fclose(output_file);
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3716kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: 0
Accepted
time: 273ms
memory: 3656kb
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:
ok 262144 numbers
Test #3:
score: 0
Accepted
time: 597ms
memory: 3632kb
input:
524288 0000000000000000000 1000000000000000000 0100000000000000000 1100000000000000000 0010000000000000000 1010000000000000000 0110000000000000000 1110000000000000000 0001000000000000000 1001000000000000000 0101000000000000000 1101000000000000000 0011000000000000000 1011000000000000000 0111000000000...
output:
1 19 19 20 19 19 20 21 19 19 19 21 20 20 21 22 19 19 19 20 19 19 21 22 20 20 20 22 21 21 22 23 19 19 19 20 19 19 20 22 19 19 19 22 21 21 22 23 20 20 20 20 20 20 22 23 21 21 21 23 22 22 23 24 19 19 19 20 19 19 20 21 19 19 19 21 20 20 22 23 19 19 19 20 19 19 22 23 21 21 21 23 22 22 23 24 20 20 20 20 2...
result:
ok 524288 numbers
Test #4:
score: 0
Accepted
time: 616ms
memory: 3656kb
input:
952358 0011101111 101010101101 101111111010100 0101011000110001100 0101111101110 010 100100000111011 011010110110011 1010111 1 1111101010 11111011001111110 0101101101011 001 1100111100 100011 10 10 0001 011100 1100010 111111101010010 01001111110011011 01100 1010 10101111 01000111100011111110 10101 0...
output:
11 12 18 20 14 3 21 16 7 1 10 18 13 3 11 4 2 2 4 4 8 18 19 6 2 8 24 5 1 1 5 25 1 14 1 15 20 3 7 24 12 10 20 21 23 1 22 18 22 5 1 6 18 12 1 4 5 12 13 12 21 1 5 12 21 8 1 8 18 4 1 12 13 6 3 3 16 6 8 1 1 17 1 1 1 6 6 4 4 10 7 5 4 5 24 6 11 4 8 15 3 9 9 19 5 16 11 5 6 9 17 1 25 14 6 1 4 20 1 4 20 14 14 ...
result:
ok 952358 numbers
Test #5:
score: 0
Accepted
time: 805ms
memory: 3896kb
input:
645561 001000111110000 01001111000 01011010 110000110111111111 0110100010000100 0 010011 010011111 00000111100001101110101100001 10111111000 100 1101100000001010110110101 1001111101 000100 101 1110101100011111101001111 000111100111100 1111001101011000000 100101001001001010111011011111 10111001100111...
output:
19 14 4 21 18 1 4 11 37 13 3 34 11 6 3 29 21 27 38 24 13 22 26 39 26 31 10 22 1 17 34 40 18 9 29 31 11 3 28 15 20 27 29 16 2 23 18 31 5 14 33 18 1 1 18 7 36 17 34 1 18 6 16 20 19 3 18 36 21 23 21 4 30 12 4 35 16 18 35 2 25 20 31 17 26 24 3 24 6 26 25 17 13 7 24 27 25 18 22 10 20 4 6 23 17 30 16 22 3...
result:
ok 645561 numbers
Test #6:
score: 0
Accepted
time: 986ms
memory: 3592kb
input:
488486 01101101 011000000100011010101011010 0100101110001011 00111110011110011010001101010000 11010010000010011111011010000010001 01001111001000010110 10110010011010100101010101111 10100000000001 1000010010 0 1111 10001010011011001111100101010 01010111100001011111011110101110101 11100 10101010001101...
output:
8 36 4 17 43 24 33 16 10 1 1 39 38 6 40 30 51 17 41 1 1 12 34 49 44 38 18 47 39 14 35 32 14 36 31 37 23 42 3 10 18 15 21 14 33 11 17 5 18 45 39 31 38 52 27 22 38 15 16 26 30 3 2 3 29 1 41 40 14 17 24 50 21 34 11 31 9 31 18 32 21 43 1 42 39 10 36 27 27 29 15 13 1 24 46 22 21 13 29 13 14 45 47 45 43 1...
result:
ok 488486 numbers
Test #7:
score: 0
Accepted
time: 1176ms
memory: 3728kb
input:
392510 01 00110011011000101011100 0100001010100011111011100011101011111100001110 1011001 00100110000011010 1101110 000100011000001111111100101011110111010111110 0111110000001010111110100011 0100111110000000111101110011011100100 100110000111101101101011011110001101111 111 0000001101011 11101100 10000...
output:
2 26 62 8 19 7 59 34 54 44 1 17 9 43 37 5 1 17 10 30 52 32 11 36 16 37 45 31 12 44 31 45 43 16 11 10 51 41 48 31 22 1 13 1 33 27 25 35 2 5 15 27 6 49 33 3 33 38 52 60 48 28 35 31 25 5 1 44 67 31 36 6 24 31 1 35 56 44 38 36 15 14 49 61 57 61 44 22 38 27 11 51 53 13 30 34 4 56 51 18 2 29 42 51 2 15 9 ...
result:
ok 392510 numbers
Test #8:
score: -100
Time Limit Exceeded
input:
197451 00010111010000100100110001010100100100101001111001000101101001010111100010110101001000001 0000011 11010001011010010010001000011111001110110100111110000010101100110000001100111 0110101110000001001010100 10100010 0110100111 010101000100011001110000000110000000011101010011101 0000110110101011001...
output:
98 8 112 30 8 12 63 96 73 123 10 106 5 106 28 24 101 44 85 109 69 29 104 76 106 111 126 8 106 89 44 36 9 56 9 111 28 37 94 6 102 2 116 19 5 50 32 17 97 114 127 12 26 9 90 133 109 9 66 27 95 1 59 59 93 40 139 84 51 33 101 127 27 1 86 12 95 41 90 134 87 24 109 57 11 85 60 50 4 52 133 66 58 15 41 45 11...