QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#330683#8058. Binary vs Ternaryucup-team1191#TL 939ms5332kbC++206.9kb2024-02-17 17:47:312024-02-17 17:47:31

Judging History

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

  • [2024-02-17 17:47:31]
  • 评测
  • 测评结果:TL
  • 用时:939ms
  • 内存:5332kb
  • [2024-02-17 17:47:31]
  • 提交

answer

/*
    author:  Maksim1744
    created: 17.02.2024 12:19:04
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#define OSTREAM(...)    ;
#define OSTREAM0(...)   ;
#endif

map<pair<string, char>, vector<pair<int, int>>> paths;

void test_case(int test) {
    string a, b;
    cin >> a >> b;
    if (b.size() == 1) {
        if (a.size() == 1) {
            cout << 0 << '\n';
            return;
        } else {
            cout << -1 << '\n';
            return;
        }
    }
    if (a.size() == 1) {
        cout << -1 << '\n';
        return;
    }
    vector<pair<int, int>> ans;
    auto op_to_string = [&](string& s, int l, int r) {
        int num = 0;
        for (int i = l; i <= r; ++i)
            num = num * 3 + (s[i] - '0');
        static string tmp;
        tmp.clear();
        while (num || tmp.empty()) {
            tmp.pb('0' + num % 2);
            num /= 2;
        }
        reverse(tmp.begin(), tmp.end());
        s.erase(s.begin() + l, s.begin() + r + 1);
        s.insert(s.begin() + l, tmp.begin(), tmp.end());
    };
    auto op = [&](int l, int r) {
        ans.eb(l, r);
        op_to_string(a, l, r);
    };

    auto get_path_to = [&](string s, auto is_good) {
        queue<string> q;
        map<string, pair<string, pair<int, int>>> par;
        par[s] = mp("", mp(0, 0));
        q.push(s);
        string found;
        while (!q.empty()) {
            string s = q.front();
            if (is_good(s)) {
                found = s;
                break;
            }
            q.pop();
            for (int l = 0; l < s.size(); ++l) {
                for (int r = l + 1; r < s.size(); ++r) {
                    auto t = s;
                    op_to_string(t, l, r);
                    if (par.count(t)) continue;
                    par[t] = mp(s, mp(l, r));
                    q.push(t);
                }
            }
        }
        assert(!found.empty());
        vector<pair<int, int>> path;
        while (found != s) {
            const auto& t = par[found];
            found = t.first;
            path.pb(t.second);
        }
        reverse(path.begin(), path.end());
        return path;
    };

    auto get_path_to_string = [&](string s, string t) {
        return get_path_to(std::move(s), [&](const string& u) {
            return t == u;
        });
    };

    auto get_path = [&](string s, char to) -> const vector<pair<int, int>>& {
        auto it = paths.find(make_pair(s, to));
        if (it != paths.end())
            return it->second;

        return paths[{s, to}] = get_path_to(s, [&](const string& t) {
            return t.size() == 4 && t[0] == '1' && t.back() == to;
        });
    };

    // for (int i = 0; i < 4; ++i) {
    //     for (int j = 1; j < 4; j += 2) {
    //         string s = "1";
    //         for (int t = 0; t < 2; ++t) {
    //             s.pb('0' + ((i >> t) & 1));
    //         }
    //         string t;
    //         for (int u = 0; u < 2; ++u) {
    //             t.pb('0' + ((j >> u) & 1));
    //         }
    //         cerr << "for " << s << " -> " << t << endl;
    //         show(get_path_to_string(s, t));
    //     }
    // }

    if (a.size() == 2) {
        if (a == "10") {
            op(0, 1);
        }
        op(0, 1);
    }

    while (b.size() > 2) {
        show(a, b);
        if (a.back() == b.back() && a.size() >= 4) {
            a.pop_back();
            b.pop_back();
            continue;
        }
        assert(a.size() >= 3);
        if (a[a.size() - 3] == '0') {
            int ind = a.size() - 3;
            while (a[ind] == '0')
                --ind;
            op(ind + 1, a.size() - 2);
        }
        assert(a.size() >= 3);
        assert(a[a.size() - 3] == '1');
        const auto& v = get_path(a.substr(a.size() - 3, 3), b.back());
        for (auto [l, r] : v) {
            op(l + a.size()-3, r + a.size()-3);
        }
        assert(a.back() == b.back());
        assert(a.size() >= 4);
    }

    while (a.size() > 4) {
        int ind = find(a.begin(), a.end(), '0') - a.begin();
        if (ind + 1 < a.size()) {
            op(ind, ind + 1);
            continue;
        }
        op(0, 1);
    }

    auto path = get_path_to_string(a, b);
    for (auto [l, r] : path)
        op(l, r);
    assert(a == b);
    cout << ans.size() << '\n';
    ++ans;
    for (auto p : ans)
        cout << p << '\n';
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);

    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        test_case(test);
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3644kb

input:

3
1
111
110110
1101010
1111
111111

output:

-1
4
2 4
1 2
1 3
2 4
5
1 2
1 3
1 3
1 3
2 4

result:

ok Haitang Suki (3 test cases)

Test #2:

score: 0
Accepted
time: 344ms
memory: 5332kb

input:

1000
11100
111
1
11110
10001
10
1011
1111
10
1110
1100
11
11010
11
110
11
1
10001
10110
10
10
11111
10000
1001
10
1
11
10111
11
10
1
100
11
10100
1
10
101
11
1100
110
11
1110
1
1001
1
11111
10
10010
10
11001
110
1010
10011
1110
10100
1001
1001
101
100
1
1001
11
101
11
101
1001
1
1
1011
1
10
10
1011
...

output:

6
3 5
4 5
1 2
2 5
1 2
2 4
-1
4
2 3
2 4
1 2
2 3
4
1 2
1 2
1 3
2 4
7
1 2
1 2
1 2
1 2
1 3
1 3
2 4
3
1 2
2 5
1 2
5
3 4
1 2
2 5
1 2
2 4
3
1 2
2 4
1 2
-1
4
2 3
1 4
1 6
2 9
7
1 2
1 2
1 3
1 3
1 3
1 3
2 4
5
2 4
1 3
1 2
1 2
2 3
-1
5
1 2
1 3
1 3
1 3
2 3
2
1 2
2 3
-1
7
1 2
1 2
1 2
1 2
1 2
1 3
2 3
-1
1
2 3
3
1 2...

result:

ok Haitang Suki (1000 test cases)

Test #3:

score: 0
Accepted
time: 309ms
memory: 5092kb

input:

1000
1110
110
1
11
1110
110
101
111
100
1001
10
110
1001
10011
10
11010
100
111
1101
1001
1
111
1
1
11
1
1011
1101
1
11
111
10
100
1110
1001
10
10
11
1
10
11
11
111
1100
10100
11001
10
110
1001
101
1
10011
100
10
10110
111
1100
110
11010
11
1000
1101
1010
1
1100
100
11010
1
1011
1
11001
1110
11
1110...

output:

2
1 2
2 4
-1
2
1 2
2 4
4
1 2
1 2
1 3
2 4
4
1 3
1 2
1 2
2 3
6
1 2
1 2
1 2
1 2
1 3
2 4
4
1 3
1 2
1 2
2 3
9
1 2
1 2
1 2
1 2
1 3
1 2
1 2
1 3
2 4
3
1 3
1 3
2 4
2
1 2
2 3
-1
0
-1
2
1 3
2 3
-1
3
2 3
1 2
2 5
5
1 2
1 2
1 3
1 3
2 4
3
2 4
1 2
2 3
4
1 2
1 2
1 3
2 4
-1
3
1 2
1 3
2 4
4
2 3
1 2
1 3
2 4
2
3 5
2 3
6...

result:

ok Haitang Suki (1000 test cases)

Test #4:

score: 0
Accepted
time: 939ms
memory: 5100kb

input:

1000
11110010
11110001
11010110
1001
11011110
1001110
111101100
111111100
110
101111001
10000100
10
1100100
101010
100
1000
1011110
110101
1001
10001111
100011111
1001111011
1110100
1010
1001100001
1000
11
101101
1000001111
11100
101001101
1
11
111001111
100101101
10
1
10111
1111000111
1101
10111
11...

output:

10
5 7
5 6
4 5
3 4
3 4
2 3
2 4
1 2
2 5
1 2
10
7 8
6 7
3 4
4 5
1 2
2 3
2 3
1 4
1 6
2 9
5
2 4
1 2
2 5
1 2
2 3
9
4 5
3 4
3 5
3 5
4 5
1 2
2 5
1 2
2 4
11
2 3
1 2
1 2
1 2
1 2
1 2
1 3
1 3
1 3
1 3
2 3
6
2 3
2 3
2 3
2 3
1 2
2 5
6
3 5
3 4
2 3
2 3
1 2
2 3
5
1 2
1 2
1 2
1 2
2 3
9
6 7
5 6
5 6
4 5
2 3
1 2
2 5
1 2...

result:

ok Haitang Suki (1000 test cases)

Test #5:

score: -100
Time Limit Exceeded

input:

1000
11
10
1111101
10
1011010001
1101010100
101110
101101001
110
110
100001
1111
10
1001100
1101101000
1
1
1110
11
1110000
110111010
1001000
110100000
1
1110101001
10
11111
1001101
110
1110
111
11
11000
1
111111
1
101111111
1100100100
101101100
1111110
10001
1001
1100110100
100
1001
101001
100011000...

output:

2
1 2
2 3
11
6 7
1 2
2 3
2 3
1 2
2 3
2 3
1 2
1 3
1 6
2 9
5
7 9
6 8
6 8
1 3
2 3
7
5 6
4 5
1 3
1 2
1 2
1 3
2 3
3
1 2
1 3
2 4
4
2 4
1 3
1 3
2 4
11
1 2
1 2
1 2
1 2
1 2
1 2
1 3
1 3
1 2
1 2
2 3
-1
-1
12
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 3
1 3
2 4
10
6 8
6 8
3 4
1 2
2 3
2 3
1 2
1 3
1 6
2 9
-1
14
4 5
5 ...

result: