結果
問題 |
No.2740 Old Maid
|
ユーザー |
|
提出日時 | 2025-04-03 16:34:11 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 134 ms / 2,000 ms |
コード長 | 1,743 bytes |
コンパイル時間 | 1,266 ms |
コンパイル使用メモリ | 100,764 KB |
実行使用メモリ | 15,616 KB |
最終ジャッジ日時 | 2025-04-03 16:34:23 |
合計ジャッジ時間 | 10,283 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 62 |
ソースコード
#include <iostream> #include <vector> #include <set> #include <iterator> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> P(n); for (int i = 0; i < n; i++) { cin >> P[i]; } // Sに1からnまでの数字を格納(自動的にソートされる) set<int> S; for (int i = 1; i <= n; i++) { S.insert(i); } // D[i] = {left, right} (初期値は-1) vector<pair<int, int>> D(n + 1, {-1, -1}); for (int i = 0; i < n; i++) { if (i > 0) { D[P[i]].first = P[i - 1]; } if (i < n - 1) { D[P[i]].second = P[i + 1]; } } vector<int> ans; while (!S.empty()) { // Sの最小要素を取得 auto it = S.begin(); int now = *it; // もしnowの右隣が-1ならSの2番目の要素をnowにする if (D[now].second == -1) { auto it2 = next(S.begin()); now = *it2; } ans.push_back(now); int rightElem = D[now].second; ans.push_back(rightElem); // now と rightElem を集合Sから削除 S.erase(now); S.erase(rightElem); // 連結リストのように左右の要素をつなげる int left = D[now].first; int rightNeighbor = D[rightElem].second; // D[P[i+1]][1]に相当 if (left != -1) { D[left].second = rightNeighbor; } if (rightNeighbor != -1) { D[rightNeighbor].first = left; } } // 結果を出力 for (int a : ans) { cout << a << " "; } cout << "\n"; return 0; }