結果

問題 No.2740 Old Maid
ユーザー mealymealy
提出日時 2024-04-21 13:36:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,758 bytes
コンパイル時間 5,709 ms
コンパイル使用メモリ 312,724 KB
実行使用メモリ 7,696 KB
最終ジャッジ日時 2024-10-13 12:15:15
合計ジャッジ時間 9,894 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 62
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using mint = atcoder::modint998244353;
//using mint = atcoder::modint1000000007;
using namespace atcoder;
using namespace std;

using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vm = vector<mint>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vpi = vector<pair<int,int>>;
using vpl = vector<pair<ll,ll>>;
#define rep(i,n) for (ll i = 0; i < n; i++)
#define replr(i,l,r) for (ll i = l; i < r; i++)
set<char> abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
const int inf =1e9;
const ll infl = 4e18;

template <typename T>
bool chmax(T &a, const T& b) {if (a < b) {a = b;  return true;}return false;}
template <typename T>
bool chmin(T &a, const T& b) {if (a > b) {a = b;  return true;}return false;}
template <typename T>
T max(vector<T> &v) {T m = 0;for (auto x : v) chmax(m, x);return m;}
template <typename T>
T min(vector<T> &v) {T m = inf;for (auto x : v) chmin(m, x);return m;}
template <typename T>
void print(vector<T> &v){for(auto x : v){cout<<x<<' ';}cout<<'\n';}
/*memo
一番若いものを選び続ける
*/


int main(){
    int N; cin >> N;
    vi p(N);rep(i,N) cin >> p[i];
    vi pos(N);
    vi nex(N,-1);
    vi pre(N,-1);
    rep(i,N-1) nex[i] =i+1;
    replr(i,1,N) pre[i] = i-1;
    rep(i,N) pos[p[i]-1] = i;
    vi q;
    rep(i,N){
        int x=pos[i];
        if (p[x]==-1) continue;
        if (nex[x]==-1) continue;
        q.push_back(p[x]);
        q.push_back(p[nex[x]]);
        if (pre[x]!=-1) nex[pre[x]]=nex[nex[x]];
        if (nex[nex[x]]!=-1) pre[nex[nex[x]]]=pre[x];
        p[x]=-1;
        p[nex[x]]=-1;
    }
    print(q);
}
0