62082023-11-08 09:24:30Error42Diana and Numberscpp17Wrong answer 0/10070ms26688 KiB
#include <algorithm>
#include <iostream>
#include <optional>
#include <vector>

using namespace std;

struct snext {
    vector<int> s;
    vector<int> next;
    vector<bool> deleted;

    int size() const {
        return s.size();
    }

    snext(string const& x) : s(x.size()), next(x.size()), deleted(x.size(), false) {
        for (int i = 0; i < size(); i++)
            s[i] = x[i] - '0';

        next.back() = 0;
        for (int i = size() - 2; i >= 0; i--) {
            if (s[i] != s[i + 1])
                next[i] = s[i + 1];
            else
                next[i] = next[i + 1];
        }
    }

    int rem() const {
        int ret = 0;

        for (int i = 0; i < size(); i++)
            if (!deleted[i])
                ret = (ret + s[i]) % 3;

        return ret;
    }

    string to_string() const {
        string ret;

        for (int i = 0; i < size(); i++)
            if (!deleted[i])
                ret.push_back(s[i] + '0');

        return ret;
    }
};

optional<snext> remove_backward(snext num, int const mod, int cnt, int const start) {
    for (int i = num.s.size() - 1; i >= start; i--) {
        if (!num.deleted[i] && num.s[i] % 3 == mod) {
            num.deleted[i] = true;
            cnt--;

            if (cnt == 0)
                return num;
        }
    }

    return nullopt;
}

optional<snext> remove_forward(snext num, int const mod, int cnt, int const start) {
    for (int i = start; i < num.s.size(); i++) {
        if (!num.deleted[i] && num.s[i] % 3 == mod && num.next[i] > num.s[i]) {
            num.deleted[i] = true;
            cnt--;

            if (cnt == 0)
                return num;
        }
    }

    return remove_backward(num, mod, cnt, start);
}

optional<snext> remove_beginning(snext num, int const mod, int cnt_total, int cnt_beginning) {
    int start = 0;

    while (true) {
        if (cnt_beginning == 0)
            break;

        if (num.s[start] % 3 != mod)
            return nullopt;

        num.deleted[start] = true;
        start++;

        while (start < num.s.size() && num.s[start] == 0) {
            num.deleted[start] = true;
            start++;
        }

        if (start == num.size())
            return nullopt;

        cnt_total--;
        cnt_beginning--;
    }

    if (cnt_total == 0)
        return num;

    start++;
    if (start == num.size())
        return nullopt;

    return remove_forward(num, mod, cnt_total, start);
}

string solve(string const& x) {
    snext const s = x;

    int const r = s.rem();
    int const q = 3 - r;

    if (r == 0)
        return x;

    optional<snext> const r0 = remove_beginning(s, r, 1, 0);
    optional<snext> const r1 = remove_beginning(s, r, 1, 1);

    optional<snext> const q0 = remove_beginning(s, q, 2, 0);
    optional<snext> const q1 = remove_beginning(s, q, 2, 1);
    optional<snext> const q2 = remove_beginning(s, q, 2, 2);

    optional<snext> const best = max(
        { r0, r1, q0, q1, q2 },
        [](optional<snext> const& a, optional<snext> const& b) {
            if (!a && !b)
                return false;
            if (!a)
                return true;
            if (!b)
                return false;

            if (a->size() < b->size())
                return true;
            if (a->size() > b->size())
                return false;

            return a->to_string() < b->to_string();
        }
    );

    if (best) {
        string const ret = best->to_string();
        return ret;
    }
    else
        return "-1";
}

void solve() {
    string s;
    cin >> s;

    cout << solve(s) << "\n";
}

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

    int t;
    cin >> t;

    while (t--)
        solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1896 KiB
subtask20/9
2Wrong answer3ms2116 KiB
3Wrong answer3ms2304 KiB
subtask30/12
4Wrong answer3ms2676 KiB
5Wrong answer3ms2892 KiB
6Wrong answer3ms3240 KiB
7Wrong answer3ms3120 KiB
8Wrong answer3ms3000 KiB
9Wrong answer3ms3136 KiB
subtask40/27
10Wrong answer70ms20380 KiB
subtask50/52
11Wrong answer18ms6428 KiB
12Wrong answer43ms7704 KiB
13Wrong answer57ms20260 KiB
14Wrong answer57ms21396 KiB
15Wrong answer35ms11040 KiB
16Wrong answer61ms25932 KiB
17Wrong answer45ms24044 KiB
18Wrong answer35ms13640 KiB
19Wrong answer37ms24984 KiB
20Wrong answer50ms26688 KiB
21Wrong answer3ms14028 KiB
22Wrong answer3ms13936 KiB
23Wrong answer3ms13976 KiB
24Wrong answer3ms13988 KiB
25Wrong answer3ms13948 KiB
26Wrong answer3ms13944 KiB