結果

問題 No.1635 Let’s Sort Integers!!
ユーザー SSRSSSRS
提出日時 2021-07-30 22:01:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,760 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 176,512 KB
実行使用メモリ 56,376 KB
最終ジャッジ日時 2023-10-14 04:48:23
合計ジャッジ時間 25,034 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 1 ms
4,352 KB
testcase_48 AC 1 ms
4,348 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 2 ms
4,352 KB
testcase_60 AC 72 ms
4,348 KB
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 AC 5 ms
6,936 KB
testcase_69 AC 1 ms
4,348 KB
testcase_70 AC 72 ms
4,348 KB
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 AC 5 ms
6,876 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();
  for (int i = 0; i < N; i++){
    cout << A[i] << ' ';
  }
  cout << endl;
  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