結果

問題 No.2740 Old Maid
ユーザー CleyLCleyL
提出日時 2024-04-21 10:38:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 2,774 bytes
コンパイル時間 1,809 ms
コンパイル使用メモリ 140,752 KB
実行使用メモリ 5,944 KB
最終ジャッジ日時 2024-04-21 10:38:12
合計ジャッジ時間 7,115 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
5,648 KB
testcase_01 AC 85 ms
5,644 KB
testcase_02 AC 83 ms
5,648 KB
testcase_03 AC 83 ms
5,656 KB
testcase_04 AC 83 ms
5,528 KB
testcase_05 AC 84 ms
5,780 KB
testcase_06 AC 87 ms
5,776 KB
testcase_07 AC 83 ms
5,656 KB
testcase_08 AC 81 ms
5,516 KB
testcase_09 AC 85 ms
5,648 KB
testcase_10 AC 80 ms
5,780 KB
testcase_11 AC 82 ms
5,944 KB
testcase_12 AC 67 ms
5,376 KB
testcase_13 AC 63 ms
5,376 KB
testcase_14 AC 14 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 52 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 61 ms
5,376 KB
testcase_19 AC 26 ms
5,376 KB
testcase_20 AC 76 ms
5,376 KB
testcase_21 AC 17 ms
5,376 KB
testcase_22 AC 46 ms
5,376 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 25 ms
5,376 KB
testcase_25 AC 78 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 8 ms
5,376 KB
testcase_28 AC 45 ms
5,376 KB
testcase_29 AC 43 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 86 ms
5,376 KB
testcase_32 AC 30 ms
5,376 KB
testcase_33 AC 81 ms
5,376 KB
testcase_34 AC 44 ms
5,376 KB
testcase_35 AC 28 ms
5,376 KB
testcase_36 AC 26 ms
5,376 KB
testcase_37 AC 36 ms
5,376 KB
testcase_38 AC 23 ms
5,376 KB
testcase_39 AC 13 ms
5,376 KB
testcase_40 AC 54 ms
5,376 KB
testcase_41 AC 71 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 1 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 1 ms
5,376 KB
testcase_58 AC 1 ms
5,376 KB
testcase_59 AC 2 ms
5,376 KB
testcase_60 AC 2 ms
5,376 KB
testcase_61 AC 2 ms
5,376 KB
testcase_62 AC 1 ms
5,376 KB
testcase_63 AC 2 ms
5,376 KB
testcase_64 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;


const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);


P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};


template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/
bool comp(pair<int,int> A, pair<int,int> B){
  return A.first < B.first;
}

int main(){
  int n;cin>>n;
  vector<int> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i];A[i]--;
  }
  stack<int> X;
  vector<pair<int,int>> ZZ;
  vector<bool> lck(n, false);
  lck[A[n-1]] = true;
  int nw = (lck[0] ? 1 : 0);
  for(int i = 0; n > i; i++){
    if(A[i] == nw){
      ZZ.push_back({A[i], A[i+1]});
      lck[A[i]] = true;
      lck[A[i+1]] = true;
      i++;
      while(nw != n && lck[nw])nw++;
      while(i+1 != n && X.size() && X.top() == nw){
        ZZ.push_back({X.top(), A[i+1]});
        lck[X.top()] = true;
        lck[A[i+1]] = true;
        i++;
        X.pop();
        while(nw != n && lck[nw])nw++;

      }
    }else{
      X.push(A[i]);
      if(X.size() >= 2){
        int second = X.top();X.pop();
        int first = X.top(); X.pop();
        if(first < second){
          ZZ.push_back({first,second});
          lck[first] = true;
          lck[second] = true;
        }else{
          X.push(first);
          X.push(second);
        }
      }
    }
  }
  while(X.size()){
    int second = X.top(); X.pop();
    int first = X.top(); X.pop();
    ZZ.push_back({first, second});
  }
  sort(ZZ.begin(), ZZ.end(), comp);
  for(int i = 0; ZZ.size() > i; i++){
    cout << ZZ[i].first+1 << " " << ZZ[i].second+1 << " \n"[i+1 == ZZ.size()];
  }
  return 0;
}
0