結果
| 問題 |
No.1635 Let’s Sort Integers!!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-07-30 21:31:27 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 472 ms / 2,000 ms |
| コード長 | 4,477 bytes |
| コンパイル時間 | 1,653 ms |
| コンパイル使用メモリ | 157,292 KB |
| 実行使用メモリ | 7,108 KB |
| 最終ジャッジ日時 | 2024-06-22 11:59:38 |
| 合計ジャッジ時間 | 23,177 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 77 |
ソースコード
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) {
}
}