結果

問題 No.1440 The Quiz Competition
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-03-26 22:39:09
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,784 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 114,648 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-04 12:26:20
合計ジャッジ時間 3,231 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 5 ms
4,384 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 40 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 48 ms
4,376 KB
testcase_09 AC 49 ms
4,380 KB
testcase_10 AC 52 ms
4,504 KB
testcase_11 AC 53 ms
4,380 KB
testcase_12 AC 50 ms
4,380 KB
testcase_13 AC 52 ms
4,376 KB
testcase_14 AC 55 ms
4,376 KB
testcase_15 AC 56 ms
4,380 KB
testcase_16 AC 56 ms
4,380 KB
testcase_17 AC 57 ms
4,380 KB
testcase_18 AC 37 ms
4,380 KB
testcase_19 AC 40 ms
4,376 KB
testcase_20 AC 41 ms
4,380 KB
testcase_21 AC 35 ms
4,380 KB
testcase_22 AC 75 ms
4,380 KB
testcase_23 AC 74 ms
4,376 KB
testcase_24 AC 75 ms
4,380 KB
testcase_25 AC 74 ms
4,384 KB
testcase_26 AC 77 ms
4,376 KB
testcase_27 AC 77 ms
4,376 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)); }


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) {
  }
}
0