結果
問題 | No.608 God's Maze |
ユーザー |
|
提出日時 | 2017-12-13 20:24:29 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 6,973 bytes |
コンパイル時間 | 2,788 ms |
コンパイル使用メモリ | 210,868 KB |
最終ジャッジ日時 | 2025-01-05 05:15:55 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 62 TLE * 3 |
ソースコード
#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 > 1000) { 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 > 1000) { 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; }