QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#133032#6628. Flip it and Stick itCyanmond#0 0ms0kbC++145.5kb2023-08-01 13:38:582024-07-04 01:04:47

Judging History

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

  • [2024-07-04 01:04:47]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-01 13:38:58]
  • 提交

answer

#include <bits/stdc++.h>
// #include "atcoder/modint"

using i64 = long long;
// using Fp = atcoder::modint998244353;

int solve(std::string S, std::string T) {
    const int N = (int)S.size();
    if (T.size() == 1) {
        const auto s = std::count(S.begin(), S.end(), T[0]);
        return s ? -1 : 0;
    }
    std::vector<std::pair<char, int>> P;
    {
        int last = 0;
        for (int i = 0; i < N; ++i) {
            if (S[i] != S[last]) {
                P.push_back({S[last], i - last});
                last = i;
            }
        }
        P.push_back({S[last], N - last});
    }
    const int U = (int)P.size();
    if (T.size() == 2 and T[0] != T[1]) {
        i64 ans = 0;
        for (int i = 0; i < U; ++i) {
            if (i != 0 and P[i].first == T[1]) ++ans;
        }
        return ans;
    } else if (T.size() == 2) {
        int cnta = 0, cntb = 0;
        for (const auto e : S) {
            if (e == T[0]) ++cnta;
            else ++cntb;
        }
        if (cntb + 1 < cnta) {
            return -1;
        } else {
            int ans = 0;
            for (int i = 0; i < N - 1; ++i) {
                if (S[i] == S[i + 1] and S[i] == T[0]) ++ans;
            }
            return ans;
        }
        return 0;
    }

    assert(T.size() == 3);
    if (T[0] != T[2]) {
        if (T[1] == T[2]) {
            std::reverse(T.begin(), T.end());
            std::reverse(S.begin(), S.end());
            std::reverse(P.begin(), P.end());
        }
        if (T[0] == '1') {
            for (auto &e : S) {
                if (e == '0') e = '1';
                else e = '0';
            }
            for (auto &e : T) {
                if (e == '0') e = '1';
                else e = '0';
            }
            for (auto &[e, len] : P) {
                if (e == '0') e = '1';
                else e = '0';
            }
        }
        int ans = 0;
        for (int i = 0; i < N - 2; ++i) {
            if (S.substr(i, 3) == T) {
                ++ans;
            }
        }
        return ans;
    }
    if (T[0] != T[1]) {
        assert(T[0] == T[2] and T[0] != T[1]);
        if (T[0] == '1') {
            for (auto &e : S) {
                if (e == '0') e = '1';
                else e = '0';
            }
            for (auto &e : T) {
                if (e == '0') e = '1';
                else e = '0';
            }
            for (auto &[e, len] : P) {
                if (e == '0') e = '1';
                else e = '0';
            }
        }
        int cnt = 0;
        for (int i = 0; i < U; ++i) {
            if (i != 0 and i != U - 1 and P[i].first == T[1] and P[i].second == 1) ++cnt;
        }
        return (cnt + 1) / 2;
        return 0;
    } else {
        assert(T[0] == T[1] and T[1] == T[2]);
        int cnta = 0, cntb = 0;
        for (int i = 0; i < N; ++i) {
            if (S[i] == T[0]) ++cnta;
            else ++cntb;
        }
        if (cntb * 2 + 2 < cnta) {
            return -1;
        }

        int cntx = 0, cnty = 0, capx = 0, capy = 0, ou = 0, nd = 0;
        for (int i = 0; i < U; ++i) {
            if (P[i].first != T[0]) continue;
            if (P[i].second % 2 == 0) {
                ++cntx;
                capx += P[i].second;
            } else {
                ++cnty;
                capy += P[i].second;
                if (P[i].second != 1) ++nd;
            }
        }
        ou = nd / 2 + ((nd % 2 == 1 and cnty != nd) ? 1 : 0);
        for (int i = 0; i < N - 1; ++i) {
            if (S[i] == S[i + 1] and S[i] != T[0]) {
                ++ou;
            }
        }
        if (S[0] != T[0]) ++ou;
        if (S[N - 1] != T[0]) ++ou;

        int ans = capx / 2 - cntx + capy / 2 - cnty / 2;
        int g = capx / 2 - cntx + capy / 2 - cnty / 2 - nd / 2 - ((nd % 2 == 1 and cnty != nd) ? 1 : 0);
        if (g > ou) ans += g - ou;
        return ans;
    }
    return -1;
}

int naive(std::string S, std::string T) {
    // assert(T == "000");
    const int N = (int)S.size();

    auto rev = [&](std::string u, int l, int r) {
        std::reverse(u.begin() + l, u.begin() + r);
        return u;
    };

    auto isOk = [&](std::string u) {
        for (int i = 0; i < N - 2; ++i) {
            if (u.substr(i, 3) == T) return false;
        }
        return true;
    };

    std::map<std::string, int> ma;
    ma[S] = 0;
    std::queue<std::string> que;
    que.push(S);
    while (not que.empty()) {
        const auto f = que.front();
        que.pop();
        if (isOk(f)) {
            return ma[f];
        }
        for (int l = 0; l < N; ++l) {
            for (int r = l + 1; r <= N; ++r) {
                auto u = rev(f, l, r);
                if (ma.find(u) == ma.end()) {
                    ma[u] = ma[f] + 1;
                    que.push(u);
                }
            }
        }
    }
    return -1;
}

std::mt19937 mt(3971);

int main() {
    /*
    std::string S, T;
    std::cin >> S >> T;
    std::cout << solve(S, T) << std::endl;
    */

    std::string T = "000";
    int test = 10000;
    while (test--) {
        std::string S(15, '0');
        for (int i = 0; i < 15; ++i) {
            if (mt() % 2 == 0) S[i] = '1';
        }
        const auto a = solve(S, T), b = naive(S, T);
        //std::cerr << S << ' ' << a << ' ' << b << std::endl;
        if (a != b) {
            std::cerr << S << ' ' << a << ' ' << b << std::endl;
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

1
0

output:


result:


Subtask #2:

score: 0
Time Limit Exceeded

Test #8:

score: 0
Time Limit Exceeded

input:

0
01

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #40:

score: 0
Time Limit Exceeded

input:

11
011

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #53:

score: 0
Time Limit Exceeded

input:

11
011

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #4:

0%