結果
問題 | No.1508 Avoid being hit |
ユーザー | SSRS |
提出日時 | 2021-05-15 00:02:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 179 ms / 3,000 ms |
コード長 | 2,666 bytes |
コンパイル時間 | 2,159 ms |
コンパイル使用メモリ | 181,116 KB |
実行使用メモリ | 14,696 KB |
最終ジャッジ日時 | 2024-10-02 07:13:28 |
合計ジャッジ時間 | 7,733 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 |
コンパイルメッセージ
main.cpp: In function 'int main()': main.cpp:102:12: warning: 'S' may be used uninitialized [-Wmaybe-uninitialized] 102 | ans[0] = S; main.cpp:94:9: note: 'S' was declared here 94 | int S; | ^
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ int N, Q; cin >> N >> Q; vector<int> A(Q); for (int i = 0; i < Q; i++){ cin >> A[i]; A[i]--; } vector<int> B(Q); for (int i = 0; i < Q; i++){ cin >> B[i]; B[i]--; } for (int i = 0; i < Q; i++){ if (A[i] > B[i]){ swap(A[i], B[i]); } } vector<vector<int>> E(Q * 2 + 2); for (int i = 0; i < Q; i++){ if (A[i] == 0){ E[Q * 2].push_back(i * 2); } if (B[i] == 0){ E[Q * 2].push_back(i * 2 + 1); } if (A[i] == N - 1){ E[i * 2].push_back(Q * 2 + 1); } if (B[i] == N - 1){ E[i * 2 + 1].push_back(Q * 2 + 1); } if (abs(A[i] - B[i]) == 1){ E[i * 2].push_back(i * 2 + 1); E[i * 2 + 1].push_back(i * 2); } if (i > 0){ if (A[i] == A[i - 1]){ E[i * 2 - 2].push_back(i * 2); E[i * 2].push_back(i * 2 - 2); } if (A[i] == B[i - 1]){ E[i * 2 - 1].push_back(i * 2); E[i * 2].push_back(i * 2 - 1); } if (B[i] == A[i - 1]){ E[i * 2 - 2].push_back(i * 2 + 1); E[i * 2 + 1].push_back(i * 2 - 2); } if (B[i] == B[i - 1]){ E[i * 2 - 1].push_back(i * 2 + 1); E[i * 2 + 1].push_back(i * 2 - 1); } } } vector<bool> used(Q * 2 + 2, false); used[Q * 2] = true; queue<int> q; q.push(Q * 2); while (!q.empty()){ int v = q.front(); q.pop(); for (int w : E[v]){ if (!used[w]){ used[w] = true; q.push(w); } } } if (used[Q * 2 + 1]){ cout << "NO" << endl; } else { vector<int> L(Q, 0), R(Q, N - 1); for (int i = Q - 1; i >= 0; i--){ if (i < Q - 1){ L[i] = max(L[i + 1] - 1, 0); R[i] = min(R[i + 1] + 1, N - 1); } if (A[i] == L[i]){ L[i]++; } if (B[i] == L[i]){ L[i]++; } if (B[i] == R[i]){ R[i]--; } if (A[i] == R[i]){ R[i]--; } } int S; for (int i = L[0] - 1; i <= R[0] + 1; i++){ if (0 <= i && i < N && i != A[0] && i != B[0]){ S = i; } } bool ok = true; vector<int> ans(Q + 1); ans[0] = S; for (int i = 0; i < Q; i++){ int K = -1; for (int j = S - 1; j <= S + 1; j++){ if (L[i] <= j && j <= R[i] && j != A[i] && j != B[i]){ K = j; } } if (K == -1){ ok = false; break; } S = K; ans[i + 1] = K; } if (!ok){ cout << "NO" << endl; } else { cout << "YES" << endl; for (int i = 0; i <= Q; i++){ cout << ans[i] + 1 << endl; } } } }