結果

問題 No.608 God's Maze
ユーザー PachicobuePachicobue
提出日時 2017-12-13 20:23:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 6,975 bytes
コンパイル時間 2,425 ms
コンパイル使用メモリ 215,116 KB
実行使用メモリ 8,376 KB
最終ジャッジ日時 2023-08-21 02:21:40
合計ジャッジ時間 7,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,332 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,504 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 127 ms
8,376 KB
testcase_30 AC 66 ms
5,728 KB
testcase_31 AC 36 ms
4,620 KB
testcase_32 AC 47 ms
5,188 KB
testcase_33 AC 71 ms
5,676 KB
testcase_34 AC 53 ms
5,220 KB
testcase_35 AC 9 ms
4,376 KB
testcase_36 AC 17 ms
4,380 KB
testcase_37 TLE -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;


template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    N--;
    string S;
    cin >> S;
    const int size = S.size();


    auto solve1 = [&]() {
        if (N > 10000) {
            return INF<int>;
        }
        vector<bool> need(size);
        set<int> st;
        for (int i = 0; i < size; i++) {
            need[i] = S[i] == '#';
            if (need[i]) {
                st.insert(i);
            }
        }
        int pos = N;
        bool l = true;
        int ans = 0;
        while (not st.empty()) {
            const int left = *st.cbegin();
            const int right = *st.crbegin();
            if (l) {
                for (; pos > left; pos--) {
                    ans++;
                    if (need[pos - 1]) {
                        st.erase(pos - 1);
                    } else {
                        st.insert(pos - 1);
                    }
                    need[pos - 1] = not need[pos - 1];
                }
            } else {
                for (; pos < right; pos++) {
                    ans++;
                    if (need[pos + 1]) {
                        st.erase(pos + 1);
                    } else {
                        st.insert(pos + 1);
                    }
                    need[pos + 1] = not need[pos + 1];
                }
            }
            l = not l;
        }
        return ans;
    };

    auto solve2 = [&]() {
        if (N > 10000) {
            return INF<int>;
        }
        vector<bool> need(size);
        set<int> st;
        for (int i = 0; i < size; i++) {
            need[i] = S[i] == '#';
            if (need[i]) {
                st.insert(i);
            }
        }
        int pos = N;
        bool l = false;
        int ans = 0;
        while (not st.empty()) {
            const int left = *st.cbegin();
            const int right = *st.crbegin();
            if (l) {
                for (; pos > left; pos--) {
                    ans++;
                    if (need[pos - 1]) {
                        st.erase(pos - 1);
                    } else {
                        st.insert(pos - 1);
                    }
                    need[pos - 1] = not need[pos - 1];
                }
            } else {
                for (; pos < right; pos++) {
                    ans++;
                    if (need[pos + 1]) {
                        st.erase(pos + 1);
                    } else {
                        st.insert(pos + 1);
                    }
                    need[pos + 1] = not need[pos + 1];
                }
            }
            l = not l;
        }
        return ans;
    };

    auto solve3 = [&]() {
        vector<bool> need(size);
        set<int> st;
        for (int i = 0; i < size; i++) {
            need[i] = S[i] == '#';
            if (need[i]) {
                st.insert(i);
            }
        }
        int pos = N;
        bool l = true;
        int ans = 0;
        int cnt = 0;
        while (not st.empty() and cnt < 2) {
            const int left = *st.cbegin();
            const int right = *st.crbegin();
            if (l) {
                for (; pos > left; pos--) {
                    ans++;
                    if (need[pos - 1]) {
                        st.erase(pos - 1);
                    } else {
                        st.insert(pos - 1);
                    }
                    need[pos - 1] = not need[pos - 1];
                }
            } else {
                for (; pos < right; pos++) {
                    ans++;
                    if (need[pos + 1]) {
                        st.erase(pos + 1);
                    } else {
                        st.insert(pos + 1);
                    }
                    need[pos + 1] = not need[pos + 1];
                    if (need[pos]) {
                        ans += (pos == right - 1 ? 1 : 2);
                        need[pos] = not need[pos];
                        st.erase(pos);
                        need[pos + 1] = (pos == right - 1 ? need[pos + 1] : not need[pos + 1]);
                        if (need[pos + 1]) {
                            st.insert(pos + 1);
                        }
                    }
                }
            }
            l = not l;
            cnt++;
        }
        return ans;
    };

    auto solve4 = [&]() {
        vector<bool> need(size);
        set<int> st;
        for (int i = 0; i < size; i++) {
            need[i] = S[i] == '#';
            if (need[i]) {
                st.insert(i);
            }
        }
        int pos = N;
        bool l = false;
        int ans = 0;
        int cnt = 0;
        while (not st.empty() and cnt < 2) {
            const int left = *st.cbegin();
            const int right = *st.crbegin();
            if (l) {
                for (; pos > left; pos--) {
                    ans++;
                    if (need[pos - 1]) {
                        st.erase(pos - 1);
                    } else {
                        st.insert(pos - 1);
                    }
                    need[pos - 1] = not need[pos - 1];
                    if (need[pos]) {
                        ans += (pos == left + 1 ? 1 : 2);
                        need[pos] = not need[pos];
                        st.erase(pos);
                        need[pos - 1] = (pos == left + 1 ? need[pos - 1] : not need[pos - 1]);
                        if (pos > left + 1) {
                            st.insert(pos - 1);
                        }
                    }
                }
            } else {
                for (; pos < right; pos++) {
                    ans++;
                    if (need[pos + 1]) {
                        st.erase(pos + 1);
                    } else {
                        st.insert(pos + 1);
                    }
                    need[pos + 1] = not need[pos + 1];
                }
            }
            l = not l;
            cnt++;
        }
        return ans;
    };

    cout << min({solve1(), solve2(), solve3(), solve4()}) << endl;
    return 0;

    //    cout << ans << endl;

    return 0;
}
0