結果
問題 | No.2114 01 Matching |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-11-01 12:23:32 |
言語 | D (dmd 2.106.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,695 bytes |
コンパイル時間 | 1,250 ms |
コンパイル使用メモリ | 137,928 KB |
実行使用メモリ | 63,296 KB |
最終ジャッジ日時 | 2024-06-22 16:34:37 |
合計ジャッジ時間 | 16,049 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 216 ms
39,700 KB |
testcase_03 | AC | 56 ms
15,764 KB |
testcase_04 | AC | 79 ms
17,792 KB |
testcase_05 | WA | - |
testcase_06 | AC | 227 ms
42,176 KB |
testcase_07 | AC | 173 ms
28,224 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 189 ms
34,416 KB |
testcase_11 | AC | 292 ms
62,116 KB |
testcase_12 | AC | 295 ms
62,512 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 201 ms
35,704 KB |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 293 ms
62,488 KB |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 296 ms
60,880 KB |
testcase_27 | AC | 323 ms
61,216 KB |
testcase_28 | AC | 162 ms
34,288 KB |
testcase_29 | AC | 311 ms
61,368 KB |
testcase_30 | AC | 184 ms
29,496 KB |
testcase_31 | AC | 318 ms
61,172 KB |
testcase_32 | WA | - |
testcase_33 | AC | 222 ms
40,352 KB |
testcase_34 | AC | 131 ms
28,832 KB |
testcase_35 | AC | 114 ms
21,572 KB |
testcase_36 | AC | 295 ms
61,072 KB |
testcase_37 | WA | - |
testcase_38 | AC | 170 ms
27,540 KB |
testcase_39 | WA | - |
testcase_40 | AC | 112 ms
31,244 KB |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | AC | 224 ms
42,756 KB |
testcase_44 | WA | - |
testcase_45 | AC | 256 ms
63,296 KB |
testcase_46 | WA | - |
testcase_47 | AC | 298 ms
61,368 KB |
testcase_48 | AC | 287 ms
61,944 KB |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | AC | 169 ms
28,320 KB |
testcase_52 | AC | 196 ms
45,316 KB |
ソースコード
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)); } // 1-dimensional min cost matching (perfect for as) long solve(const(long[]) as, const(long[]) bs) { debug { writeln("solve ", as, " ", bs); } const asLen = cast(int)(as.length); const bsLen = cast(int)(bs.length); assert(asLen <= bsLen); if (asLen == 0) { return 0; } alias Entry = Tuple!(long, "pos", int, "typ"); auto es = new Entry[asLen + bsLen]; foreach (i; 0 .. asLen) es[i] = Entry(as[i], 0); foreach (i; 0 .. bsLen) es[asLen + i] = Entry(bs[i], 1); es.sort; enum inf = 1000; // const inf = es[asLen + bsLen - 1].pos - es[0].pos + 1; long f0; // list of slopes, ls: as surplus long offL, offR; DList!long ls, rs; ls.insertBack(-inf); rs.insertBack(+inf); foreach (i; 0 .. asLen + bsLen) { if (es[i].typ == 0) { // shift to left const t = rs.front + offR; rs.removeFront; if (rs.empty) rs.insertFront(+inf - offR); f0 += t; ls.insertBack(t - offL); } else { // shift to right, chmin for 0->1 if (rs.front < 0) { f0 += rs.front; ls.back += rs.front; rs.front = 0; } const t = ls.back + offL; ls.removeBack; if (ls.empty) ls.insertBack(-inf - offL); f0 -= t; rs.insertFront(t - offR); } debug { foreach (t; ls) write(t + offL, " "); write("; ", f0, "; "); foreach (t; rs) write(t + offR, " "); writeln; } if (i < asLen + bsLen - 1) { offL -= (es[i + 1].pos - es[i].pos); offR += (es[i + 1].pos - es[i].pos); } debug { foreach (t; ls) write(t + offL, " "); write("; ", f0, "; "); foreach (t; rs) write(t + offR, " "); writeln; } } return f0; } void main() { try { for (; ; ) { int M = readInt; int N = readInt; const K = readLong; auto A = new long[M]; foreach (i; 0 .. M) A[i] = readLong; auto B = new long[N]; foreach (i; 0 .. N) B[i] = readLong; A.sort; B.sort; if (M > N) { swap(M, N); swap(A, B); } long[][long] ass, bss; foreach (i; 0 .. M) ass[A[i] % K] ~= A[i] / K; foreach (i; 0 .. N) bss[B[i] % K] ~= B[i] / K; long ans; foreach (key, as; ass) { if (key !in bss) { ans = -1; break; } auto bs = bss[key]; if (as.length > bs.length) { ans = -1; break; } ans += solve(as, bs); } writeln(ans); } } catch (EOFException e) { } }