結果
問題 | No.1361 [Zelkova 4th Tune *] QUADRUPLE-SEQUENCEの詩 |
ユーザー |
👑 |
提出日時 | 2021-01-22 21:53:43 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 492 ms / 2,000 ms |
コード長 | 4,070 bytes |
コンパイル時間 | 2,224 ms |
コンパイル使用メモリ | 167,952 KB |
実行使用メモリ | 21,324 KB |
最終ジャッジ日時 | 2024-06-22 10:41:53 |
合計ジャッジ時間 | 16,861 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 74 |
ソースコード
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)); }void main() {try {for (; ; ) {auto N = new int[4];foreach (h; 0 .. 4) {N[h] = readInt();}const S = readLong();auto A = new long[][4];foreach (h; 0 .. 4) {A[h] = new long[N[h]];foreach (i; 0 .. N[h]) {A[h][i] = readLong();}}alias Entry = Tuple!(long, "val", int, "i", int, "j");Entry[] fs, gs;foreach (i; 0 .. N[0]) foreach (j; 0 .. N[1]) {fs ~= Entry(A[0][i] * A[1][j], i, j);}foreach (i; 0 .. N[2]) foreach (j; 0 .. N[3]) {gs ~= Entry(A[2][i] * A[3][j], i, j);}fs.sort;gs.sort;const fsLen = cast(int)(fs.length);const gsLen = cast(int)(gs.length);const int lbF = fs.lowerBound(Entry(0, int.min, int.min));const int ubF = fs.upperBound(Entry(0, int.max, int.max));const int lbG = gs.lowerBound(Entry(0, int.min, int.min));const int ubG = gs.upperBound(Entry(0, int.max, int.max));// <= tlong calc(long t) {long ret;if (0 <= t) {ret += 1L * (ubF - lbF) * (gsLen - 0);ret += 1L * (fsLen - 0) * (ubG - lbG);ret -= 1L * (ubF - lbF) * (ubG - lbG);ret += 1L * (lbF - 0) * (gsLen - ubG);ret += 1L * (fsLen - ubF) * (lbG - 0);for (int i = lbF, j = 0; j < lbG; ++j) {for (; i > 0 && fs[i - 1].val * gs[j].val <= t; --i) {}ret += (lbF - i);}for (int i = ubF, j = gsLen; --j >= ubG; ) {for (; i < fsLen && fs[i].val * gs[j].val <= t; ++i) {}ret += (i - ubF);}} else {for (int i = 0, j = ubG; j < gsLen; ++j) {for (; i < lbF && fs[i].val * gs[j].val <= t; ++i) {}ret += (i - 0);}for (int i = fsLen, j = lbG; --j >= 0; ) {for (; i > ubF && fs[i - 1].val * gs[j].val <= t; --i) {}ret += (fsLen - i);}}return ret;}long lo = -10L^^18, hi = +10L^^18;for (; lo + 1 < hi; ) {const mid = (lo + hi) / 2;((calc(mid) >= S) ? hi : lo) = mid;}writeln(hi);if (hi == 0) {writefln("%s %s %s %s",A[0][fs[lbF].i], A[1][fs[lbF].j],A[2][gs[lbG].i], A[3][gs[lbG].j]);} else {foreach (ref g; gs) {if (g.val != 0 && hi % g.val == 0) {const tar = hi / g.val;const pos = fs.lowerBound(Entry(tar, int.min, int.min));if (pos < fsLen && fs[pos].val == tar) {writefln("%s %s %s %s",A[0][fs[pos].i], A[1][fs[pos].j],A[2][g.i], A[3][g.j]);break;}}}}}} catch (EOFException e) {}}