QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#482651 | #4273. Good Game | peaneval_kala | 0 | 17ms | 42944kb | C++23 | 10.7kb | 2024-07-17 20:26:13 | 2024-07-17 20:26:15 |
Judging History
answer
#pragma GCC optimize(3, "unroll-loops", "no-stack-protector")
#define atsum(l, r) accumulate(l, r, 0)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 0x3f3f3f3f;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
inline void chkmin(T &x, T y) {
x = min(x, y);
}
template <typename T>
inline void chkmax(T &x, T y) {
x = max(x, y);
}
namespace FastIO {
// ------------------------------
#define IN_HAS_NEG
#define OUT_HAS_NEG
#define CHK_EOF
#define DISABLE_MMAP
// ------------------------------
#if __cplusplus < 201400
#error Please use C++14 or higher.
#endif
#if __cplusplus > 201700
#define INLINE_V inline
#else
#define INLINE_V
#endif
#if (defined(LOCAL) || defined(_WIN32)) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifndef DISABLE_MMAP
#include <sys/mman.h>
#endif
#ifdef LOCAL
inline char gc() { return getchar(); }
inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
INLINE_V constexpr int _READ_SIZE = 1 << 18;
INLINE_V static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr,
*_read_ptr_end = nullptr;
inline char gc() {
if (__builtin_expect(_read_ptr == _read_ptr_end, false)) {
_read_ptr = _read_buffer;
_read_ptr_end =
_read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
#ifdef CHK_EOF
if (__builtin_expect(_read_ptr == _read_ptr_end, false)) return EOF;
#endif
}
return *_read_ptr++;
}
#else
INLINE_V static const char *_read_ptr =
(const char *)mmap(nullptr, INT_MAX, 1, 2, 0, 0);
inline char gc() { return *_read_ptr++; }
#endif
INLINE_V constexpr int _WRITE_SIZE = 1 << 18;
INLINE_V static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
inline void pc(char c) {
*_write_ptr++ = c;
if (__builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false)) {
fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
_write_ptr = _write_buffer;
}
}
INLINE_V struct _auto_flush {
~_auto_flush() {
fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
}
} _auto_flush;
#endif
#ifdef CHK_EOF
inline bool _isdigit(char c) { return (c & 16) && c != EOF; }
inline bool _isgraph(char c) { return c > 32 && c != EOF; }
#else
inline bool _isdigit(char c) { return c & 16; }
inline bool _isgraph(char c) { return c > 32; }
#endif
template <class T>
INLINE_V constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <class T>
INLINE_V constexpr bool _is_signed = numeric_limits<T>::is_signed;
template <class T>
INLINE_V constexpr bool _is_unsigned = _is_integer<T> && !_is_signed<T>;
template <>
INLINE_V constexpr bool _is_integer<__int128> = true;
template <>
INLINE_V constexpr bool _is_integer<__uint128_t> = true;
template <>
INLINE_V constexpr bool _is_signed<__int128> = true;
template <>
INLINE_V constexpr bool _is_unsigned<__uint128_t> = true;
#undef INLINE_V
inline void read(char &c) {
do c = gc();
while (!_isgraph(c));
}
inline void read_cstr(char *s) {
char c = gc();
while (!_isgraph(c)) c = gc();
while (_isgraph(c)) *s++ = c, c = gc();
*s = 0;
}
inline void read(string &s) {
char c = gc();
s.clear();
while (!_isgraph(c)) c = gc();
while (_isgraph(c)) s.push_back(c), c = gc();
}
#ifdef IN_HAS_NEG
template <class T, enable_if_t<_is_signed<T>, int> = 0>
inline void read(T &x) {
char c = gc();
bool f = true;
x = 0;
while (!_isdigit(c)) {
if (c == 45) f = false;
c = gc();
}
if (f)
while (_isdigit(c)) x = x * 10 + (c & 15), c = gc();
else
while (_isdigit(c)) x = x * 10 - (c & 15), c = gc();
}
template <class T, enable_if_t<_is_unsigned<T>, int> = 0>
#else
template <class T, enable_if_t<_is_integer<T>, int> = 0>
#endif
inline void read(T &x) {
char c = gc();
while (!_isdigit(c)) c = gc();
x = 0;
while (_isdigit(c)) x = x * 10 + (c & 15), c = gc();
}
inline void write(char c) { pc(c); }
inline void write_cstr(const char *s) {
while (*s) pc(*s++);
}
inline void write(const string &s) {
for (char c : s) pc(c);
}
#ifdef OUT_HAS_NEG
template <class T, enable_if_t<_is_signed<T>, int> = 0>
inline void write(T x) {
char buffer[numeric_limits<T>::digits10 + 1];
int digits = 0;
if (x >= 0) do
buffer[digits++] = (x % 10) | 48, x /= 10;
while (x);
else {
pc(45);
do buffer[digits++] = -(x % 10) | 48, x /= 10;
while (x);
}
while (digits) pc(buffer[--digits]);
}
template <class T, enable_if_t<_is_unsigned<T>, int> = 0>
#else
template <class T, enable_if_t<_is_integer<T>, int> = 0>
#endif
inline void write(T x) {
char buffer[numeric_limits<T>::digits10 + 1];
int digits = 0;
do buffer[digits++] = (x % 10) | 48, x /= 10;
while (x);
while (digits) pc(buffer[--digits]);
}
template <int N>
struct _tuple_io_helper {
template <class... T>
static inline void _read(tuple<T...> &x) {
_tuple_io_helper<N - 1>::_read(x), read(get<N - 1>(x));
}
template <class... T>
static inline void _write(const tuple<T...> &x) {
_tuple_io_helper<N - 1>::_write(x), pc(32), write(get<N - 1>(x));
}
};
template <>
struct _tuple_io_helper<1> {
template <class... T>
static inline void _read(tuple<T...> &x) {
read(get<0>(x));
}
template <class... T>
static inline void _write(const tuple<T...> &x) {
write(get<0>(x));
}
};
template <class... T>
inline void read(tuple<T...> &x) {
_tuple_io_helper<sizeof...(T)>::_read(x);
}
template <class... T>
inline void write(const tuple<T...> &x) {
_tuple_io_helper<sizeof...(T)>::_write(x);
}
template <class T1, class T2>
inline void read(pair<T1, T2> &x) {
read(x.first), read(x.second);
}
template <class T1, class T2>
inline void write(const pair<T1, T2> &x) {
write(x.first), pc(32), write(x.second);
}
template <class T1, class... T2>
inline void read(T1 &x, T2 &...y) {
read(x), read(y...);
}
template <class... T>
inline void read_cstr(char *x, T *...y) {
read_cstr(x), read_cstr(y...);
}
template <class T1, class... T2>
inline void write(const T1 &x, const T2 &...y) {
write(x), write(y...);
}
template <class... T>
inline void write_cstr(const char *x, const T *...y) {
write_cstr(x), write_cstr(y...);
}
template <class T>
inline void print(const T &x) {
write(x);
}
inline void print_cstr(const char *x) { write_cstr(x); }
template <class T1, class... T2>
inline void print(const T1 &x, const T2 &...y) {
print(x), pc(32), print(y...);
}
template <class... T>
inline void print_cstr(const char *x, const T *...y) {
print_cstr(x), pc(32), print_cstr(y...);
}
inline void println() { pc(10); }
inline void println_cstr() { pc(10); }
template <class... T>
inline void println(const T &...x) {
print(x...), pc(10);
}
template <class... T>
inline void println_cstr(const T *...x) {
print_cstr(x...), pc(10);
}
} // namespace FastIO
using namespace FastIO;
template <typename T>
inline void clear(T &x) {
T y;
swap(x, y);
}
const int N = 1e6 + 10;
int l[N], r[N], n;
char str[N];
pair<int, int> block[N];
int lb[N], rb[N], bc;
inline void delp(int x) { l[r[x]] = l[x], r[l[x]] = r[x]; }
inline void delpb(int x) {
lb[rb[x]] = lb[x], rb[lb[x]] = rb[x];
}
vector<pair<int, int> > res;
inline int realdel(int x) {
// cerr << "find del:" << ' ' << x << ' ' << lb[x] << ' ' << block[lb[x]].second << ' ' << block[x].second << ' '<< block[x].first << endl;
int c = block[lb[x]].second + 1, cnt = 0;
int kl = lb[x];
for (int i = block[x].first; ; i = r[i]) {
delp(i), ++cnt;
// cerr << "find cdel:" << i << endl;
if (i == block[x].second) break;
}
// cerr << "find cnt qwq:" << cnt << endl;
if (cnt % 3 == 2) res.emplace_back(c, 2), cnt -= 2;
if (cnt % 3 == 0) {
while (cnt) res.emplace_back(c, 3), cnt -= 3;
} else if (cnt % 3 == 1) {
cnt -= 4, res.emplace_back(c, 2), res.emplace_back(c, 2);
while (cnt) res.emplace_back(c, 3), cnt -= 3;
}
r[block[lb[x]].second] = block[rb[x]].first;
l[block[rb[x]].first] = block[lb[x]].second;
block[lb[x]].second = block[rb[x]].second;
delpb(x), delpb(rb[x]);
return kl;
}
inline bool judge(int x) { return block[x].first == block[x].second; }
int pb[N], cb[N], sp[N];
inline void work(int L, int R) {
// cerr << "find work:" << L << ' ' << R << endl;
int mid = L + R >> 1;
int pl = mid, pr = mid;
assert(R ^ L ^ 1 & 1);
while (judge(pl)) pl--;
while (judge(pr)) pr++;
int c = mid - pl;
// cerr << "find c:" << c << endl;
int cc = R - L + 1;
while (c--) pr = realdel(pr), cc -= 2;
while (cc > -1) pl = realdel(pl), cc -= 2;
assert(cc == -1);
}
int main() {
read(n), read_cstr(str + 1);
l[n + 1] = n, r[0] = 1;
for (int i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1;
for (int l = 1; l <= n;) {
int r = l;
while (r < n && str[r + 1] == str[l]) ++r;
block[++bc] = {l, r}, lb[bc] = bc - 1, rb[bc] = bc + 1;
l = r + 1;
}
lb[bc + 1] = bc, rb[0] = 1;
for (int i = bc; i; i--)
pb[i] = block[i].first == block[i].second ? pb[i + 1] + 1 : 0;
for (int i = 1; i <= bc; i++)
sp[i] = block[i].first == block[i].second ? sp[i - 1] + 1 : 0;
block[bc + 1] = {n + 1, n + 1};
// cerr << "after there" << endl;
if (bc & 1 ^ 1) {
for (int i = 1; i <= bc; i += 2)
if (!(sp[i] >= (i >> 1) && judge(i + 1 >> 1)) &&
!(pb[i + 1] >= (bc - i >> 1) && judge(bc + i + 1 >> 1))) {
// cerr << "find i:" << i << endl;
work(i + 1, bc);
// cerr << "find r1:" << r[1] << ' ' << block[1].second << endl;
if (block[i].second == n + 1) block[i].second = l[block[i].second], rb[i] = bc + 1, lb[bc + 1] = i;
work(1, i);
println(res.size());
for (auto [l, r] : res) println(l, r);
return 0;
}
return println("-1"s), 0;
} else {
if (pb[1] >= (bc >> 1) &&
block[(bc >> 1) + 1].first == block[(bc >> 1) + 1].second)
return println("-1"s), 0;
work(1, bc);
println(res.size());
for (auto [l, r] : res) println(l, r);
}
return 0;
}
/*
9
ABAABBBAA
*/
/*
2 2 1
15
AABABAABABAABAB
2 11121112111
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 3
Accepted
time: 2ms
memory: 13848kb
input:
9 ABAABBBAA
output:
4 3 2 2 2 2 2 1 3
result:
ok good solution!
Test #2:
score: 0
Accepted
time: 2ms
memory: 13872kb
input:
13 ABABBABABBABA
output:
6 9 2 8 2 4 2 3 2 2 3 1 2
result:
ok good solution!
Test #3:
score: 0
Accepted
time: 1ms
memory: 11988kb
input:
15 AABABAABABAABAB
output:
7 11 2 10 2 6 2 5 2 4 3 3 2 1 2
result:
ok good solution!
Test #4:
score: -3
Runtime Error
input:
15 ABAABBBABAABBAB
output:
result:
Subtask #2:
score: 0
Runtime Error
Test #51:
score: 0
Runtime Error
input:
299 ABABABABABABABABABABABABABABABABABBAABBAABBAABBAAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBAABBBAABBAABBAABBAABBAABBAABBAABBAABBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #102:
score: 7
Accepted
time: 0ms
memory: 13940kb
input:
5998 BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
2997 3995 3 3995 3 3995 3 3994 2 3993 2 3992 2 3991 2 3990 2 3989 2 3988 2 3987 2 3986 2 3985 2 3984 2 3983 2 3982 2 3981 2 3980 2 3979 2 3978 2 3977 2 3976 2 3975 2 3974 2 3973 2 3972 2 3971 2 3970 2 3969 2 3968 2 3967 2 3966 2 3965 2 3964 2 3963 2 3962 2 3961 2 3960 2 3959 2 3958 2 3957 2 3956 2 3...
result:
ok good solution!
Test #103:
score: 0
Accepted
time: 0ms
memory: 11932kb
input:
5999 BBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
2998 4000 2 4000 3 3999 2 3998 2 3997 2 3996 2 3995 2 3994 2 3993 2 3992 2 3991 2 3990 2 3989 2 3988 2 3987 2 3986 2 3985 2 3984 2 3983 2 3982 2 3981 2 3980 2 3979 2 3978 2 3977 2 3976 2 3975 2 3974 2 3973 2 3972 2 3971 2 3970 2 3969 2 3968 2 3967 2 3966 2 3965 2 3964 2 3963 2 3962 2 3961 2 3960 2 3...
result:
ok good solution!
Test #104:
score: -7
Time Limit Exceeded
input:
5998 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #153:
score: 9
Accepted
time: 17ms
memory: 42944kb
input:
999997 ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...
output:
499996 666661 2 666661 2 666661 3 666661 3 666660 2 666659 2 666658 2 666657 2 666656 2 666655 2 666654 2 666653 2 666652 2 666651 2 666650 2 666649 2 666648 2 666647 2 666646 2 666645 2 666644 2 666643 2 666642 2 666641 2 666640 2 666639 2 666638 2 666637 2 666636 2 666635 2 666634 2 666633 2 66663...
result:
ok good solution!
Test #154:
score: 0
Accepted
time: 15ms
memory: 41948kb
input:
999998 BBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA...
output:
499996 666667 2 666667 3 666666 2 666665 2 666664 2 666663 2 666662 2 666661 2 666660 2 666659 2 666658 2 666657 2 666656 2 666655 2 666654 2 666653 2 666652 2 666651 2 666650 2 666649 2 666648 2 666647 2 666646 2 666645 2 666644 2 666643 2 666642 2 666641 2 666640 2 666639 2 666638 2 666637 2 66663...
result:
ok good solution!
Test #155:
score: -9
Time Limit Exceeded
input:
999997 BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...