結果
| 問題 |
No.608 God's Maze
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-13 20:41:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 82 ms / 2,000 ms |
| コード長 | 4,631 bytes |
| コンパイル時間 | 2,441 ms |
| コンパイル使用メモリ | 207,296 KB |
| 最終ジャッジ日時 | 2025-01-05 05:16:18 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 65 |
ソースコード
#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;
}