結果

問題 No.608 God's Maze
ユーザー PachicobuePachicobue
提出日時 2017-12-13 20:41:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 4,631 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 212,000 KB
実行使用メモリ 8,136 KB
最終ジャッジ日時 2023-08-21 02:22:28
合計ジャッジ時間 5,224 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 61 ms
8,136 KB
testcase_30 AC 68 ms
5,584 KB
testcase_31 AC 36 ms
4,612 KB
testcase_32 AC 48 ms
5,040 KB
testcase_33 AC 72 ms
5,660 KB
testcase_34 AC 54 ms
5,148 KB
testcase_35 AC 9 ms
4,384 KB
testcase_36 AC 18 ms
4,380 KB
testcase_37 AC 21 ms
4,380 KB
testcase_38 AC 52 ms
5,064 KB
testcase_39 AC 9 ms
4,376 KB
testcase_40 AC 24 ms
4,384 KB
testcase_41 AC 22 ms
4,380 KB
testcase_42 AC 73 ms
5,684 KB
testcase_43 AC 18 ms
4,380 KB
testcase_44 AC 62 ms
5,332 KB
testcase_45 AC 58 ms
5,360 KB
testcase_46 AC 11 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 64 ms
5,432 KB
testcase_49 AC 18 ms
4,376 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1 ms
4,380 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,384 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 1 ms
4,380 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 1 ms
4,384 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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 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);
            }
        }

        if (st.size() == 1 and st.find(N) != st.end()) {
            return 3;
        }

        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);
            }
        }
        if (st.size() == 1 and st.find(N) != st.end()) {
            return 3;
        }
        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({solve3(), solve4()}) << endl;
    return 0;

    //    cout << ans << endl;

    return 0;
}
0