QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#459892#8523. Puzzle IIucup-team4361TL 1362ms35808kbC++209.2kb2024-06-30 16:25:542024-06-30 16:25:55

Judging History

This is the latest submission verdict.

  • [2024-06-30 16:25:55]
  • Judged
  • Verdict: TL
  • Time: 1362ms
  • Memory: 35808kb
  • [2024-06-30 16:25:54]
  • Submitted

answer

#include <bits/stdc++.h>

using std::bitset;
using std::cin, std::cout, std::endl, std::flush, std::cerr;
using std::min, std::max;
using std::optional;
using std::pair, std::tuple;
using std::set, std::map, std::multiset;
using std::tie, std::swap;
using std::vector, std::array, std::queue, std::deque, std::string;
using std::views::iota, std::views::reverse;

template <class A> constexpr int sz(A&& a) {
    return int(std::size(std::forward<A>(a)));
}

using i32 = int32_t;
using i64 = int64_t;

auto mt =
    std::mt19937(std::chrono::steady_clock::now().time_since_epoch().count());

template <class T> using Vec = std::vector<T>;

template <class M, bool persistent = false> struct TreapManager {
    using S = typename M::S;
    using F = typename M::F;

    TreapManager(M m_, int alloc = 0) : m(m_) {
        if (alloc > 0) {
            nodes.reserve(alloc);
        } else {
            // make sure to understand what you're doing
            assert(!persistent);
        }

        for (int z = 0; z < 2; z++) {
            states[z] = uint32_t(mt());
        }
    }

    using Tree = int;

    Tree make_empty() { return Tree(null); }

    Tree make_single(S s) {  /// start-hash
        int i = int(nodes.size());
        nodes.push_back(Node{null, null, 1, false, false, s, s, m.id()});
        return i;
    }  /// end-hash

    Tree make_copy(Tree o) { return _make_copy(o); }

    int size(const Tree t) { return _size(t); }
    int reverse(Tree t) { return _reverse(t); }
    int apply(Tree t, F f) { return _apply(t, f); }
    S prod(const Tree& t) { return _prod(t); }

    Tree split_k(Tree& t, int k) {  /// start-hash
        Tree o;
        tie(t, o) = _split_k(t, k);
        return o;
    }  /// end-hash

    Tree merge(Tree a, Tree b) { return _merge(a, b); }

    Tree build(const Vec<S>& a) {  /// start-hash
        if (a.empty()) return make_empty();
        return _build(a, 0, int(a.size()));
    }  /// end-hash

    Vec<S> to_array(const Tree& t) {  /// start-hash
        Vec<S> buf;
        buf.reserve(size(t));
        _to_array(t, buf);
        return buf;
    }  /// end-hash

  private:
    static constexpr int null = -42;
    M m;

    struct Node {  /// start-hash
        int li, ri, sz;
        bool rev, app;
        S a, s;
        F f;
    };
    Vec<Node> nodes;
    Node& node(int i) { return nodes[i]; }
    int _size(int i) { return i == null ? 0 : node(i).sz; }  /// end-hash

    int _make_copy(int o) {  /// start-hash
        if constexpr (!persistent) return o;

        if (o == null) return null;
        assert(nodes.size() < nodes.capacity());
        int i = int(nodes.size());
        nodes.push_back(node(o));
        return i;
    }  /// end-hash

    int _build(const Vec<S>& a, int l, int r) {  /// start-hash
        if (r - l == 1) {
            return make_single(a[l]);
        }
        int md = (l + r) / 2;
        return _merge(_build(a, l, md), _build(a, md, r));
    }  /// end-hash

    void _update(int i) {  /// start-hash
        auto& n = node(i);
        n.s = m.op(_prod(n.li), m.op(n.a, _prod(n.ri)));
        n.sz = size(n.li) + size(n.ri) + 1;
    }  /// end-hash

    int _reverse(int i) {  /// start-hash
        if (i == null) return i;
        i = _make_copy(i);
        auto& n = node(i);
        n.rev = !n.rev;
        swap(n.li, n.ri);
        return i;
    }  /// end-hash

    S _prod(int i) { return i == null ? m.e() : node(i).s; }

    int _apply(int i, F f) {  /// start-hash
        if (i == null) return i;
        i = _make_copy(i);
        auto& n = node(i);
        n.s = m.mapping_sz(f, n.s, n.sz);
        n.a = m.mapping_sz(f, n.a, 1);
        n.f = m.composition(f, n.f);
        n.app = true;
        return i;
    }  /// end-hash

    int downdate(int i) {  /// start-hash
        assert(i != null);
        i = _make_copy(i);
        auto& n = node(i);
        if (n.rev) {
            n.li = _reverse(n.li);
            n.ri = _reverse(n.ri);
            n.rev = false;
        }
        if (n.app) {
            n.li = _apply(n.li, n.f);
            n.ri = _apply(n.ri, n.f);
            n.f = m.id();
            n.app = false;
        }
        return i;
    }  /// end-hash

    template <class F> pair<int, int> _split(int i, F go_left) {  /// start-hash
        if (i == null) return {null, null};
        i = downdate(i);
        auto& n = node(i);
        int li = n.li, ri = n.ri;
        int x, y;
        if (go_left(li, ri)) {
            y = i;
            tie(x, n.li) = _split(n.li, go_left);
        } else {
            x = i;
            tie(n.ri, y) = _split(n.ri, go_left);
        }
        _update(i);
        return {x, y};
    }  /// end-hash

    pair<int, int> _split_k(int i, int k) {  /// start-hash
        return _split(i, [&](int li, int) -> bool {
            int lsz = size(li);
            if (k <= lsz) {
                return true;
            } else {
                k -= lsz + 1;
                return false;
            }
        });
    }  /// end-hash

    // Use std::mt19937_64 if performance is not an issue
    // https://prng.di.unimi.it/xoroshiro64star.c
    inline uint32_t rotl(const uint32_t x, int k) {  /// start-hash
        return (x << k) | (x >> (32 - k));
    }
    uint32_t states[2];
    uint32_t rng() {
        const uint32_t s0 = states[0];
        uint32_t s1 = states[1];
        const uint32_t res = s0 * 0x9E3779BB;
        s1 ^= s0;
        states[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9);
        states[1] = rotl(s1, 13);
        return res;
    }  /// end-hash

    int _merge(int a, int b) {  /// start-hash
        if (a == null) return b;
        if (b == null) return a;
        int r;
        uint32_t sa = size(a), sb = size(b);
        if (rng() % (sa + sb) < sa) {
            r = downdate(a);
            node(r).ri = _merge(node(r).ri, b);
        } else {
            r = downdate(b);
            node(r).li = _merge(a, node(r).li);
        }
        _update(r);
        return r;
    }  /// end-hash

    void _to_array(int i, Vec<S>& buf) {  /// start-hash
        if (i == null) return;
        downdate(i);
        auto& n = node(i);
        _to_array(n.li, buf);
        buf.push_back(n.a);
        _to_array(n.ri, buf);
    }  /// end-hash
};

int main() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << std::fixed << std::setprecision(20);

    int N, K;
    string A, B;
    cin >> N >> K >> A >> B;
    {
        int balance = 0;
        for (char c : A) {
            if (c == 'B') {
                balance++;
            } else if (c == 'C') {
                balance--;
            } else {
                assert(false);
            }
        }

        if (balance > 0) {
            for (char& c : A) {
                c ^= 'B' ^ 'C';
            }
            for (char& c : B) {
                c ^= 'B' ^ 'C';
            }
        }
    }

    struct Monoid {
        struct S {
            int offset;
            bool color;
            array<optional<int>, 2> locs;
        };
        using F = char;  // dummy

        S single(char x) {
            bool color = (x != 'B');
            auto s = S{};
            s.offset = 1;
            s.color = color;
            s.locs[color] = 0;
            s.locs[1 - color] = std::nullopt;
            return s;
        }

        S e() { return S{.offset = 0, .color = false, .locs = {}}; }
        S op(S a, S b) {
            auto locs = a.locs;
            for (auto c : iota(0, 2)) {
                if (locs[c]) continue;
                if (b.locs[c].has_value()) {
                    locs[c] = a.offset + b.locs[c].value();
                }
            }
            return S{.offset = a.offset + b.offset,
                     .color = false,  // don't care
                     .locs = locs};
        }
        S rev(S a) { return a; }

        F id() { return {}; }
        F composition(F, F) { return {}; }
        S mapping_sz(F, S s, int) { return s; }
    };

    auto m = Monoid();
    auto tm = TreapManager(m, 2 * N);
    using Tree = decltype(tm)::Tree;
    auto ta = tm.make_empty();
    auto tb = tm.make_empty();
    for (int i : iota(0, N)) {
        assert(i < sz(A));
        assert(i < sz(B));
        ta = tm.merge(ta, tm.make_single(m.single(A[i])));
        tb = tm.merge(tb, tm.make_single(m.single(B[i])));
    }

    auto ops = Vec<pair<int, int>>();
    auto do_rotate = [&](Tree t, int k) -> Tree {
        Tree o = tm.split_k(t, k);
        return tm.merge(o, t);
    };
    auto do_swap = [&](int i, int j) {
        i = (i % N + N) % N;
        j = (j % N + N) % N;
        ops.emplace_back(i, j);
        ta = do_rotate(ta, i);
        tb = do_rotate(tb, j);
        int a, b, c, d;
        a = ta, c = tb;
        b = tm.split_k(a, K);
        d = tm.split_k(c, K);
        ta = tm.merge(c, b);
        tb = tm.merge(a, d);
        ta = do_rotate(ta, N - i);
        tb = do_rotate(tb, N - j);
    };

    while (true) {
        auto i = tm.prod(ta).locs[0];
        auto j = tm.prod(tb).locs[1];
        assert(i.has_value() == j.has_value());
        if (!i.has_value()) break;
        do_swap(i.value() - K, j.value());
        do_swap(i.value() - K + 1, j.value());
        assert(tm.size(ta) == tm.size(tb));
        assert(tm.size(ta) == N);
    }

    cout << ops.size() << '\n';
    for (auto [i, j] : ops) {
        cout << i + 1 << ' ' << j + 1 << '\n';
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3660kb

input:

6 3
BCCBCC
BBCBBC

output:

4
4 3
5 3
2 6
3 6

result:

ok moves = 4

Test #2:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

2 1
BC
BC

output:

2
2 2
1 2

result:

ok moves = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3668kb

input:

2 1
BB
CC

output:

0

result:

ok moves = 0

Test #4:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

2 1
CC
BB

output:

0

result:

ok moves = 0

Test #5:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

3 1
CCC
BBB

output:

0

result:

ok moves = 0

Test #6:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

3 1
CBC
BCB

output:

2
1 2
2 2

result:

ok moves = 2

Test #7:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

3 2
BBB
CCC

output:

0

result:

ok moves = 0

Test #8:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

3 2
BCB
BCC

output:

2
3 1
1 1

result:

ok moves = 2

Test #9:

score: 0
Accepted
time: 0ms
memory: 3868kb

input:

4 2
CCCB
BBCB

output:

2
2 3
3 3

result:

ok moves = 2

Test #10:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

9 6
CCCBCCCBB
BBBCBBBCC

output:

6
7 4
8 4
4 7
5 7
4 7
5 7

result:

ok moves = 6

Test #11:

score: 0
Accepted
time: 0ms
memory: 3620kb

input:

21 3
CCCCBBCBCCCCCCCBCCCCC
BBCCBCBBBBBBBBBCBBBBB

output:

8
2 3
3 3
3 3
4 3
5 6
6 6
13 16
14 16

result:

ok moves = 8

Test #12:

score: 0
Accepted
time: 1ms
memory: 3672kb

input:

49 41
CBCCBCCBCCBCCBCCCBBCCBCBBCCCBBCCBCBCBCBCCCCBCBCCB
BCCCCBCBBBBCBCBBBBBCBBBBCCCCBCBBCBBCBBBBCBCBCBBBC

output:

38
10 2
11 2
9 2
10 2
13 2
14 2
16 2
17 2
9 3
10 3
23 7
24 7
9 8
10 8
28 13
29 13
33 17
34 17
34 17
35 17
38 17
39 17
9 17
10 17
41 18
42 18
9 20
10 20
45 22
46 22
9 26
10 26
3 31
4 31
4 33
5 33
8 38
9 38

result:

ok moves = 38

Test #13:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

114 8
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

output:

0

result:

ok moves = 0

Test #14:

score: 0
Accepted
time: 1ms
memory: 3676kb

input:

266 28
CBBCBBCCCCBCBBCBBBCBCBCBCBBCBCBBCCCCBCCCCCBCCBBCCBBCBCBBCCCCCCBBBCCCBCCBCBBCCCBCCCCCCBCBBCCCBCBBCCBCBBBCBCCCBBCBCCCCBBCBBCBBCCBBCCCCCBBCCCBCCCCCCCCBBBBBBCBCCBCCCCBBCBBBBCBCCCCCCCBCBBCBCCCCCCCCCCCBBBBCCCCBCBCCCBCCCCCCCCCBCBCCCBBBCCCBCCBCBBCBCCCCCCBCBCCCCBCBCCBCCCCBCB
CCBCBCBBCBCBBCBCCCBBBCBCBB...

output:

206
240 1
241 1
239 1
240 1
241 2
242 2
239 3
240 3
243 5
244 5
244 6
245 6
249 8
250 8
251 9
252 9
239 9
240 9
252 9
253 9
254 12
255 12
239 13
240 13
255 15
256 15
239 25
240 25
256 39
257 39
258 46
259 46
260 46
261 46
262 47
263 47
239 53
240 53
264 55
265 55
239 55
240 55
265 58
266 58
1 58
2 5...

result:

ok moves = 206

Test #15:

score: 0
Accepted
time: 4ms
memory: 3796kb

input:

620 443
BBBBBBCBBBCBBCBCBCBBBBCCCBCCBCBBBBBBCCCBBBCCBBCBCBCBBCCCCBCBBCBCCCCBBBBBBCCCCCBBBBCCBCBBBBBCBCBBCBCBCCCCBCBBCBBBCBBBCCCBCCCBBBBBCCBBCCBBBCCBCCBCBBCBCCCCCCCCCBCBCBBBCBBCBBCBBBBBBBCCBBBBBBBBBBCBBCBBCBBCCCBBCCBBBBCCCBBBBBBCBBBBBBBBCBBCBCBBBCCBBBBCCBBBCBCBCBBBBBCBBCBBBBCBBBBCCBBBCBBBBBCBBCCCCBCC...

output:

484
184 4
185 4
178 4
179 4
188 4
189 4
191 9
192 9
178 10
179 10
193 11
194 11
195 11
196 11
178 12
179 12
178 13
179 13
178 13
179 13
178 14
179 14
200 15
201 15
201 15
202 15
178 16
179 16
178 18
179 18
178 18
179 18
178 19
179 19
178 19
179 19
202 19
203 19
204 20
205 20
178 20
179 20
178 21
179...

result:

ok moves = 484

Test #16:

score: 0
Accepted
time: 7ms
memory: 3636kb

input:

1446 646
CCCBCCCCCCCBBCCBBCCCCBBCCCBBCCCCCCCCCCCCCCCBCCCCCCCCBBCCBBCCCBCBBBCCCCBBCCCCCCCCCCCBCBCCCBBCCCCBBCBCBCCCCCCCBCCCCCCCBCCBCBBCCCCCCCCCCCCBCCCBCCCCCCBCCCBCCCCCBBCCCBBCCCBBBCCBCCBCCBBBCBCBCCCCBCBCCCBCCCCBBCCCCCCCBCCCCBCCCBBBCCCBCCBBCCCCBCCCBBCBCCCCCBBCCBCCCCCCBCCCCCCCCCCCCCCBCCCCCBCBCCCCBCCCCCB...

output:

874
804 1
805 1
812 3
813 3
801 3
802 3
813 11
814 11
816 11
817 11
801 12
802 12
817 14
818 14
822 15
823 15
801 25
802 25
823 25
824 25
827 27
828 27
801 27
802 27
801 27
802 27
801 27
802 27
828 27
829 27
801 36
802 36
801 37
802 37
844 46
845 46
853 51
854 51
854 51
855 51
801 56
802 56
857 57
8...

result:

ok moves = 874

Test #17:

score: 0
Accepted
time: 7ms
memory: 3664kb

input:

3374 2755
BCBBCBBBCBBBBBBBBBCCBBBBBBBCCBBCBBCBBBBBCBBBBBBBBCBBBBBBBBBBBBCBBBCCBBBBCBBBBBCBBBBBCBBBBCBBBBBBBBBCBBBBBBBBBBBCBBBBBBBCBBBBBBBBBBCCBBBBBBBBBCBBCBBBCBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCBBBCBBCBBBBBBBBBBBBBBCCBCBCBCBBBBCBBBCBBBBBBBBCBBCBCBBCBCCBBBBBBBBBBBCCBBBBBBBBBBBBBBBBBCBBBBBBBBBBB...

output:

1216
621 3
622 3
624 17
625 17
628 21
629 21
638 21
639 21
620 22
621 22
620 25
621 25
639 25
640 25
647 29
648 29
648 29
649 29
651 34
652 34
654 36
655 36
660 36
661 36
620 45
621 45
669 55
670 55
682 65
683 65
686 65
687 65
687 83
688 83
692 89
693 89
620 92
621 92
698 96
699 96
704 100
705 100
7...

result:

ok moves = 1216

Test #18:

score: 0
Accepted
time: 50ms
memory: 4576kb

input:

7872 7827
BCBBCBCBBCCBCBBBCCCBBBBBBBCBBBBCCBCCBCBBBBBBCBBCBBBCCCBBBCCCCBCBBBBCBBCCBBBBCCBBCBBBCBCBBCBCBBCCBBBCCBBBBCCBBCBBBBBBCBBBBBBBBCCBBCCCCBCCCBBCCCBBCBCBBBCBBBBCBBBBCBCBBBCCBBCCCCBBBCBBCCBBBBBBCBBBBCCCBBBCCCBBCCCBBBBBBCCBBBCCCCBBBCBBCBCCBBBCCCCBCBBCCBBBBCCBBBCBBCBBBCBBBCBBBBCCBBBBBCCBCBCBBBBBBB...

output:

5928
47 1
48 1
50 3
51 3
52 4
53 4
55 4
56 4
56 4
57 4
58 8
59 8
62 8
63 8
63 11
64 11
46 11
47 11
46 12
47 12
64 12
65 12
72 12
73 12
46 12
47 12
77 14
78 14
78 14
79 14
80 19
81 19
81 22
82 22
46 27
47 27
83 28
84 28
90 30
91 30
46 32
47 32
46 35
47 35
100 35
101 35
46 35
47 35
109 35
110 35
111 3...

result:

ok moves = 5928

Test #19:

score: 0
Accepted
time: 71ms
memory: 6096kb

input:

18368 17997
CBBBBBBBBBBCBBBBBBBBBBBBBBCBBCCBBBBBBBBBBBBBCBCBBBBBBBBCBBBBBCBBBBBBBBBBBBBBCBBBBBBBBBBCBBBCBCBBBBBCBCBBCBBBBBBBBBBBBBCCCBBBBBBCBBBBCBCBBCBBBBBCBBBBBBBCCBBBBCCBCBBBBBBBBBBBBCBBBBBBBBCBCBBBBBBBBCBCBBBBBBBBBBBCBBBBCCBBBBBBBCBBBBBBBBBBBBBBBCCBBCBCBBCBCBBBCBBBBBBBBBBBBBCBBCBBBBBBBCCCBBBBBBBB...

output:

7330
372 2
373 2
383 9
384 9
398 9
399 9
401 15
402 15
402 17
403 17
416 39
417 39
418 39
419 39
427 39
428 39
433 42
434 42
372 56
373 56
448 57
449 57
459 59
460 59
463 74
464 74
465 86
466 86
471 91
472 91
473 94
474 94
476 94
477 94
490 112
491 112
491 112
492 112
492 116
493 116
499 119
500 119...

result:

ok moves = 7330

Test #20:

score: 0
Accepted
time: 105ms
memory: 9344kb

input:

42858 28689
CCCCCCCCCCCCCCCCCCCCBCCCBBCCCBCCCCCCCCCBCCCCCCCBCCCBCCCCCBCCCCCCCCBCCBCCBCCCCCCCCCCCCCCCCCBCCCCCCCCCBCCCCBCCCCCCCCCCCCCCCCCCCCCCCCCCCCBCCCCCCCCCCCCCCCCCBBCCCCCCCCCCCCCCBBCCCBCCCCCCCCCCBCCCCCCCBCCCCBCBCCCBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCBCBCCCBCBCCCCCCCCCCCCCCBCCCCCCCCBCCCCCCCCCCCCCCCCCC...

output:

8086
14190 1
14191 1
14194 2
14195 2
14170 6
14171 6
14195 10
14196 10
14199 13
14200 13
14209 24
14210 24
14217 36
14218 36
14221 36
14222 36
14227 39
14228 39
14236 46
14237 46
14239 46
14240 46
14242 71
14243 71
14260 73
14261 73
14270 75
14271 75
14275 76
14276 76
14304 87
14305 87
14322 89
1432...

result:

ok moves = 8086

Test #21:

score: 0
Accepted
time: 534ms
memory: 17500kb

input:

100002 40466
BBBBBBBCCBCBCCBCBBBBCCBBCBBBBBBBBBBCBBBBCBBBBBCBBBBBBBBBCBBBCBBBCCBCBCBBBBBBCBBBBBBBCBBBBBCBBBBCBCBCBBBBBBBBCBBBBBBBBCBCBBBBCBBBBBBBBBBBBBCBCBBCBBBBBBBBBBBCBBBBBBBCBCBCBCBBBBBBBBCBCBBBBBBBBCBBBBBBBBCBCCBBBCCBBCBBCBBBBBBBBBBCBBBCBBBBBBBBBBBBCBBCCBBCBBCBBBBCBBBBCBBBCCBBBCBBBBBBBCBBBBCBBBC...

output:

45728
59544 3
59545 3
59545 6
59546 6
59547 9
59548 9
59549 10
59550 10
59550 10
59551 10
59552 13
59553 13
59557 25
59558 25
59558 26
59559 26
59537 26
59538 26
59561 26
59562 26
59572 26
59573 26
59577 27
59578 27
59537 33
59538 33
59583 33
59584 33
59593 39
59594 39
59597 39
59598 39
59537 44
595...

result:

ok moves = 45728

Test #22:

score: 0
Accepted
time: 1362ms
memory: 35808kb

input:

233338 159967
CCBCBBCCCCBBCCCCCCCCBCCCBCCCCBCCBCCCCCCCCCBCBCCBBCBBCCCCBCCCCCCCCCCCCCCCCCCCBCCBCCBBCBCCBBBCCBCCCCBBCCCBCCCCCCCCCCCBCCBCCCCCCCCBCCCBBCBCCCBCCCCCBCCBCCBCCCCCCCBCCCCCBCCBBCCCCCCCBCCCCCCCCBBBCCCCCCCCCCCCBBBCCCBBCCBCBCCCCCCCCCBCCCCBCCCCCCCCBBCCCCBCCCCBCCCBCCCBCCCCCBCCCCCBBCCCBCCCCCCCCCCCCC...

output:

103344
73374 1
73375 1
73376 3
73377 3
73377 4
73378 4
73382 5
73383 5
73372 6
73373 6
73383 21
73384 21
73372 25
73373 25
73392 27
73393 27
73396 29
73397 29
73372 29
73373 29
73401 34
73402 34
73404 34
73405 34
73414 37
73415 37
73416 41
73417 41
73419 43
73420 43
73420 45
73421 45
73372 47
73373 ...

result:

ok moves = 103344

Test #23:

score: -100
Time Limit Exceeded

input:

300000 1
CCCBBBBBBCCBCCCBCBBBBCBCBCBBCBBBBCBCBCCBBCCBBCCBCBBCBBBBBBCBBBCBCBCCBBCBBCCCCCBCBCBBBBBBBBCBCBBBBCCCBCBBBCCBCBCBCBCBBCCBCBCCCBCBCBBCCBCCBBCBBBBCCBBCBCBBBBCCBBBBBBBCCBCCCBCBCCBBBBBCCBBBBCBCCBCBBCBBCBCCCBBBBBBBCCCCBBBBBBBBCBBBCCBCBBBBCCBBBCCBCBCCBCCBBCBBCCCCCBCBBBCCCCCCBCBBBCBBCCCCCCBCCCBBBCC...

output:

299752
3 1
4 1
4 3
5 3
5 6
6 6
6 9
7 9
7 12
8 12
8 13
9 13
11 15
12 15
15 18
16 18
17 19
18 19
18 21
19 21
19 26
20 26
20 27
21 27
22 28
23 28
24 29
25 29
26 30
27 30
27 31
28 31
29 32
30 32
30 33
31 33
31 35
32 35
32 37
33 37
34 38
35 38
36 39
37 39
39 40
40 40
40 41
41 41
43 43
44 43
44 44
45 44
4...

result: