結果
問題 | No.2114 01 Matching |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-11-01 12:36:14 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 375 ms / 5,000 ms |
コード長 | 3,690 bytes |
コンパイル時間 | 3,272 ms |
コンパイル使用メモリ | 123,992 KB |
実行使用メモリ | 60,612 KB |
最終ジャッジ日時 | 2023-09-04 18:49:09 |
合計ジャッジ時間 | 18,280 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,384 KB |
testcase_01 | AC | 2 ms
4,380 KB |
testcase_02 | AC | 256 ms
40,040 KB |
testcase_03 | AC | 69 ms
15,216 KB |
testcase_04 | AC | 98 ms
17,508 KB |
testcase_05 | AC | 81 ms
10,920 KB |
testcase_06 | AC | 274 ms
39,220 KB |
testcase_07 | AC | 201 ms
28,704 KB |
testcase_08 | AC | 259 ms
41,772 KB |
testcase_09 | AC | 202 ms
25,400 KB |
testcase_10 | AC | 227 ms
31,096 KB |
testcase_11 | AC | 360 ms
57,684 KB |
testcase_12 | AC | 356 ms
60,612 KB |
testcase_13 | AC | 208 ms
28,756 KB |
testcase_14 | AC | 219 ms
42,520 KB |
testcase_15 | AC | 221 ms
45,372 KB |
testcase_16 | AC | 304 ms
39,112 KB |
testcase_17 | AC | 236 ms
37,084 KB |
testcase_18 | AC | 156 ms
20,780 KB |
testcase_19 | AC | 206 ms
26,808 KB |
testcase_20 | AC | 365 ms
57,736 KB |
testcase_21 | AC | 252 ms
34,844 KB |
testcase_22 | AC | 165 ms
14,976 KB |
testcase_23 | AC | 197 ms
26,364 KB |
testcase_24 | AC | 60 ms
11,100 KB |
testcase_25 | AC | 56 ms
9,788 KB |
testcase_26 | AC | 360 ms
57,732 KB |
testcase_27 | AC | 357 ms
57,752 KB |
testcase_28 | AC | 182 ms
32,272 KB |
testcase_29 | AC | 360 ms
57,696 KB |
testcase_30 | AC | 202 ms
27,760 KB |
testcase_31 | AC | 361 ms
57,796 KB |
testcase_32 | AC | 257 ms
36,540 KB |
testcase_33 | AC | 258 ms
35,984 KB |
testcase_34 | AC | 148 ms
26,996 KB |
testcase_35 | AC | 135 ms
21,336 KB |
testcase_36 | AC | 375 ms
57,652 KB |
testcase_37 | AC | 209 ms
28,736 KB |
testcase_38 | AC | 207 ms
25,856 KB |
testcase_39 | AC | 111 ms
18,260 KB |
testcase_40 | AC | 137 ms
29,000 KB |
testcase_41 | AC | 246 ms
33,084 KB |
testcase_42 | AC | 189 ms
26,800 KB |
testcase_43 | AC | 275 ms
39,096 KB |
testcase_44 | AC | 293 ms
38,660 KB |
testcase_45 | AC | 303 ms
58,096 KB |
testcase_46 | AC | 77 ms
11,512 KB |
testcase_47 | AC | 357 ms
57,724 KB |
testcase_48 | AC | 355 ms
57,712 KB |
testcase_49 | AC | 207 ms
26,976 KB |
testcase_50 | AC | 96 ms
14,832 KB |
testcase_51 | AC | 200 ms
26,072 KB |
testcase_52 | AC | 222 ms
42,596 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; 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->0 const t = ls.back + offL; ls.removeBack; if (ls.empty) ls.insertBack(-inf - offL); f0 -= t; rs.insertFront(t - offR); const tt = rs.front + offR; if (tt < 0) { f0 += tt; ls.back += tt; rs.front = 0 - 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) { } }