結果
| 問題 |
No.1440 The Quiz Competition
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-26 22:39:09 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 74 ms / 2,000 ms |
| コード長 | 3,784 bytes |
| コンパイル時間 | 1,330 ms |
| コンパイル使用メモリ | 128,972 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-22 11:06:37 |
| 合計ジャッジ時間 | 3,264 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 27 |
ソースコード
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)); }
long brute(const(long) N, const(long) A, const(long) W, const(long) K) {
long ret = long.min;
void dfs(long i, long a, long w, long aLast, long[] scores) {
if (i == N) {
if (a == 0 && w == 0) {
auto ss = scores.dup;
ss.sort.reverse;
if (K == 1 || ss[K - 2] > ss[K - 1]) {
chmax(ret, ss[K - 1]);
}
}
} else {
foreach (aa; 0 .. min(a, aLast) + 1) {
foreach (ww; 0 .. w + 1) {
dfs(i + 1, a - aa, w - ww, aa, scores ~ (aa - ww * (ww + 1) / 2));
}
}
}
}
dfs(0, A, W, A, []);
return ret;
}
long solve(const(long) N, const(long) A, const(long) W, const(long) K) {
enum LIM = 10L^^18;
long ret = long.min;
if (K == 1) {
ret = A;
} else if (K == N) {
foreach (w; 0 .. 10) {
if (W >= w) {
const q = (W - w) / N, r = (W - w) % N;
const me = q + ((0 < r) ? 1 : 0) + w;
long lo = -(me * (me + 1) / 2) - 1, hi = LIM;
for (; lo + 1 < hi; ) {
const mid = (lo + hi) / 2;
BigInt need;
need += mid + (me * (me + 1) / 2);
need += BigInt(max(r - 1, 0)) * max(mid + 1 + ((q + 1) * (q + 2) / 2), 0);
need += BigInt(N - 1 - max(r - 1, 0)) * max(mid + 1 + (q * (q + 1) / 2), 0);
((need <= A) ? lo : hi) = mid;
}
debug {
// writefln("HELP %s %s %s %s: w = %s, q = %s, r = %s, me = %s, lo = %s", N, A, W, K, w, q, r, me, lo);
}
if (lo >= -(me * (me + 1) / 2)) {
chmax(ret, lo);
}
}
}
} else {
if (A >= K - 1) {
ret = (A - (K - 1)) / K;
} else if (W >= N - K + 1) {
ret = -1;
} else {
//
}
}
return ret;
}
void main() {
debug {
enum lim = 8;
foreach (n; 2 .. lim) {
stderr.writefln("testing n = %s", n);
stderr.flush;
foreach (a; 0 .. lim) foreach (w; 0 .. lim) foreach (k; 1 .. n + 1) {
const brt = brute(n, a, w, k);
const slv = solve(n, a, w, k);
if (brt != slv) {
stderr.writefln("n = %s, a = %s, w = %s, k = %s: %s %s", n, a, w, k, brt, slv);
stderr.flush;
return;
}
}
}
}
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
const N = readLong();
const A = readLong();
const W = readLong();
const K = readLong();
const ans = solve(N, A, W, K);
if (ans <= long.min) {
writeln(":(");
} else {
writeln(ans);
}
}
}
} catch (EOFException e) {
}
}