結果

問題 No.2740 Old Maid
ユーザー srjywrdnprkt
提出日時 2024-10-30 15:54:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 878 bytes
コンパイル時間 2,250 ms
コンパイル使用メモリ 202,492 KB
最終ジャッジ日時 2025-02-25 01:48:03
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int N, x, y, z, w;
    cin >> N;
    vector<int> p(N), nx(N, -1), pr(N, -1);
    set<int> st;
    vector<int> ans;
    for (int i=0; i<N; i++){
        cin >> p[i]; p[i]--;
        st.insert(i);
    }
    for (int i=0; i<N; i++){
        if (i != 0) pr[p[i]] = p[i-1];
        if (i != N-1) nx[p[i]] = p[i+1];
    }

    while(!st.empty()){
        y = *st.begin();
        if (nx[y] == -1) y = *next(st.begin());
        z = nx[y];
        w = (z == -1 ? -1 : nx[z]);
        x = pr[y];
        ans.push_back(y);
        ans.push_back(z);
        st.erase(y);
        st.erase(z);
        if (x != -1) nx[x] = w;
        if (w != -1) pr[w] = x;
    }

    for (auto x : ans) cout << x+1 << " ";
    cout << endl;

    return 0;
}
0