結果

問題 No.1635 Let’s Sort Integers!!
ユーザー SSRSSSRS
提出日時 2021-07-30 22:01:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 1,640 ms
コンパイル使用メモリ 175,752 KB
実行使用メモリ 56,316 KB
最終ジャッジ日時 2023-10-14 04:49:02
合計ジャッジ時間 20,555 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,352 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,352 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 2 ms
4,352 KB
testcase_39 AC 1 ms
4,352 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 2 ms
4,352 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 2 ms
4,356 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 1 ms
4,352 KB
testcase_47 AC 1 ms
4,348 KB
testcase_48 AC 1 ms
4,348 KB
testcase_49 AC 1 ms
4,352 KB
testcase_50 AC 1 ms
4,348 KB
testcase_51 AC 2 ms
4,352 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,352 KB
testcase_54 AC 1 ms
4,352 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,352 KB
testcase_57 AC 2 ms
4,352 KB
testcase_58 AC 1 ms
4,352 KB
testcase_59 AC 1 ms
4,348 KB
testcase_60 AC 72 ms
4,348 KB
testcase_61 AC 437 ms
49,088 KB
testcase_62 AC 108 ms
21,292 KB
testcase_63 AC 449 ms
52,532 KB
testcase_64 AC 330 ms
40,640 KB
testcase_65 AC 100 ms
20,516 KB
testcase_66 AC 97 ms
19,204 KB
testcase_67 AC 97 ms
19,248 KB
testcase_68 AC 5 ms
7,156 KB
testcase_69 AC 1 ms
4,352 KB
testcase_70 AC 72 ms
4,352 KB
testcase_71 AC 388 ms
46,240 KB
testcase_72 AC 445 ms
51,248 KB
testcase_73 AC 489 ms
56,316 KB
testcase_74 AC 244 ms
31,132 KB
testcase_75 AC 97 ms
20,656 KB
testcase_76 AC 98 ms
19,124 KB
testcase_77 AC 97 ms
19,276 KB
testcase_78 AC 5 ms
7,000 KB
testcase_79 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
void get(vector<int> A){
  int N = A.size();
  int p;
  for (int i = 0; i < N; i++){
    if (A[i] == 1 || A[i] == -1){
      p = i;
    }
  }
  vector<int> neg, pos;
  for (int i = 0; i < N; i++){
    if (i != p){
      if (A[i] == -1){
        neg.push_back(i);
      }
      if (A[i] == 1){
        pos.push_back(i);
      }
    }
  }
  for (int i = 0; i < N; i++){
    if (A[i] == -2){
      neg.push_back(i);
    }
    if (A[i] == 2){
      pos.push_back(i);
    }
  }
  set<int> st;
  for (int i = 0; i < N; i++){
    if (A[i] == 0){
      st.insert(i);
    }
  }
  vector<int> ans;
  ans.push_back(p);
  while (A[p] != 0){
    if (A[p] == -1){
      int p2 = pos.back();
      pos.pop_back();
      A[p]++;
      A[p2]--;
      while (true){
        auto itr = st.lower_bound(p);
        if (itr == st.end()){
          break;
        }
        if (*itr > p2){
          break;
        }
        ans.push_back(*itr);
        st.erase(itr);
      }
      p = p2;
    } else {
      int p2 = neg.back();
      neg.pop_back();
      A[p]--;
      A[p2]++;
      while (true){
        auto itr = st.lower_bound(p);
        if (itr == st.begin()){
          break;
        }
        itr--;
        if (*itr < p2){
          break;
        }
        ans.push_back(*itr);
        st.erase(itr);
      }
      p = p2;
    }
    ans.push_back(p);
  }
  for (int i = 0; i < N; i++){
    cout << ans[i] + 1;
    if (i < N - 1){
      cout << ' ';
    }
  }
  cout << endl;
}
int main(){
  int N;
  cin >> N;
  long long K;
  cin >> K;
  if (K < N - 1){
    cout << -1 << endl;
  } else if (K == N - 1){
    for (int i = 1; i <= N; i++){
      cout << i;
      if (i < N){
        cout << ' ';
      }
    }
    cout << endl;
  } else if (N == 2){
    cout << -1 << endl;
  } else {
    K -= N;
    vector<int> A(N, 0);
    A[0] = -1;
    A[N - 2] = -1;
    A[N - 1] = 2;
    int L = 1, R = N - 2;
    int d = 0;
    bool ok = false;
    while (R - L >= 1){
      if (K <= R - L){
        if (d == 0){
          A[R] = 0;
          A[R - K] = -1;
          get(A);
          ok = true;
          break;
        } else {
          A[L] = 0;
          A[L + K] = 1;
          get(A);
          ok = true;
          break;
        }
      }
      K -= R - L;
      if (d == 0){
        A[R] = 0;
        A[L] = -2;
        A[L + 1] = 1;
        K--;
        L++;
        d = 1;
      } else {
        A[L] = 0;
        A[R] = 2;
        A[R - 1] = -1;
        K--;
        R--;
        d = 0;
      }
    }
    if (!ok){
      if (K < N / 2){
        swap(A[0], A[K]);
        get(A);
      } else {
        cout << -1 << endl;
      }
    }
  }
}
0