結果

問題 No.1635 Let’s Sort Integers!!
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-07-30 21:31:27
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 465 ms / 2,000 ms
コード長 4,477 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 155,032 KB
実行使用メモリ 8,384 KB
最終ジャッジ日時 2023-09-04 13:32:02
合計ジャッジ時間 31,066 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,512 KB
testcase_06 AC 2 ms
4,508 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,388 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,388 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 1 ms
4,388 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,388 KB
testcase_24 AC 1 ms
4,388 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,508 KB
testcase_27 AC 1 ms
4,388 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 1 ms
4,384 KB
testcase_36 AC 1 ms
4,388 KB
testcase_37 AC 1 ms
4,384 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 1 ms
4,384 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 2 ms
4,384 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 1 ms
4,384 KB
testcase_46 AC 1 ms
4,384 KB
testcase_47 AC 2 ms
4,384 KB
testcase_48 AC 2 ms
4,384 KB
testcase_49 AC 2 ms
4,384 KB
testcase_50 AC 2 ms
4,384 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1 ms
4,384 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,384 KB
testcase_56 AC 1 ms
4,384 KB
testcase_57 AC 2 ms
4,388 KB
testcase_58 AC 2 ms
4,384 KB
testcase_59 AC 2 ms
4,380 KB
testcase_60 AC 462 ms
6,656 KB
testcase_61 AC 464 ms
6,820 KB
testcase_62 AC 463 ms
6,784 KB
testcase_63 AC 463 ms
6,776 KB
testcase_64 AC 464 ms
6,828 KB
testcase_65 AC 428 ms
7,576 KB
testcase_66 AC 428 ms
6,508 KB
testcase_67 AC 428 ms
6,532 KB
testcase_68 AC 3 ms
4,376 KB
testcase_69 AC 2 ms
4,384 KB
testcase_70 AC 465 ms
6,608 KB
testcase_71 AC 464 ms
6,816 KB
testcase_72 AC 464 ms
6,844 KB
testcase_73 AC 464 ms
6,780 KB
testcase_74 AC 465 ms
6,772 KB
testcase_75 AC 430 ms
8,384 KB
testcase_76 AC 426 ms
7,052 KB
testcase_77 AC 427 ms
6,864 KB
testcase_78 AC 2 ms
4,380 KB
testcase_79 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int[] solve(const(int) N, const(long) K) {
  int[] ans;
  if (K < N - 1) {
    //
  } else if (K <= 1L * N * (N - 1) / 2) {
    int l = 1, r = N - 1, bef = 0;
    ans ~= 0;
    long k = K - (N - 1);
    foreach_reverse (j; 1 .. N) {
      if (k >= j - 1) {
        k -= (j - 1);
        ans ~= bef = (abs(bef - l) == j) ? l++ : r--;
      } else {
        ans ~= bef = (abs(bef - l) == 1) ? l++ : r--;
      }
    }
  } else {
    long s;
    foreach (i; 0 .. N - N / 2) s -= 2 * i;
    foreach (i; N - N / 2 .. N) s += 2 * i;
    if (N % 2 == 0) {
      long mx = s;
      mx += (N / 2 - 1);
      mx -= (N / 2);
      if (K <= mx) {
        const head = (N / 2 - 1) - (mx - K);
        assert(0 <= head); assert(head < N / 2);
        ans = new int[N];
        foreach (i; 0 .. N - N / 2) ans[2 * i] = i;
        foreach (i; N - N / 2 .. N) ans[N - 1 - 2 * (i - (N - N / 2))] = i;
        swap(ans[0], ans[cast(int)(2 * head)]);
      }
    } else {
      long mx = s;
      mx += (N - N / 2 - 2);
      mx += (N - N / 2 - 1);
      if (K <= mx) {
        const head = (N - N / 2 - 2) - (mx - K);
        assert(0 <= head); assert(head <= N - N / 2 - 2);
        ans = new int[N];
        foreach (i; 0 .. N - N / 2) ans[2 * i] = i;
        foreach (i; N - N / 2 .. N) ans[2 * (i - (N - N / 2)) + 1] = i;
        swap(ans[0], ans[cast(int)(2 * head)]);
      }
    }
  }
  
  if (ans) {
    long s;
    foreach (i; 0 .. N - 1) {
      s += abs(ans[i] - ans[i + 1]);
    }
    assert(s == K, format("%s %s: %s %s", N, K, ans, s));
  }
  return ans;
}

void main() {
  debug {
    foreach (n; 2 .. 10 + 1) {
      auto perm = iota(n).array;
      auto pss = new int[][n^^2];
      do {
        int s;
        foreach (i; 0 .. n - 1) {
          s += abs(perm[i] - perm[i + 1]);
        }
        if (!pss[s]) {
          pss[s] ~= perm.dup;
        }
      } while (perm.nextPermutation);
      foreach (s; 0 .. n^^2) {
        if (pss[s] && s >= n * (n - 1) / 2) {
          writeln(n, " ", s, ": ", pss[s]);
        }
      }
    }
  }
  debug {
    foreach (n; 2 .. 10 + 1) {
      if (n % 2 == 0) {
        int s;
        foreach (i; 0 .. n - n / 2) s -= 2 * i;
        foreach (i; n - n / 2 .. n) s += 2 * i;
        int mn = s;
        mn += 0;
        mn -= (n - 1);
        int mx = s;
        mx += (n / 2 - 1);
        mx -= (n / 2);
        writeln(n, ": ", mn, " ", mx, "; ", n * (n - 1) / 2);
      } else {
        int s;
        foreach (i; 0 .. n - n / 2) s -= 2 * i;
        foreach (i; n - n / 2 .. n) s += 2 * i;
        int mn = s;
        mn += 0;
        mn += 1;
        int mx = s;
        mx += (n - n / 2 - 2);
        mx += (n - n / 2 - 1);
        writeln(n, ": ", mn, " ", mx, "; ", n * (n - 1) / 2);
      }
    }
  }
  debug {
    foreach (n; 2 .. 10 + 1) {
      int[] ks;
      foreach (k; 0 .. n^^2) {
        const res = solve(n, k);
        if (res) {
          ks ~= k;
        }
      }
      writeln(n, ": ", ks);
    }
  }
  
  try {
    for (; ; ) {
      const N = readInt();
      const K = readLong();
      
      const ans = solve(N, K);
      if (ans) {
        foreach (i; 0 .. N) {
          if (i > 0) write(" ");
          write(ans[i] + 1);
        }
        writeln;
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}
0