結果
| 問題 |
No.2114 01 Matching
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-01 12:36:14 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
CE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,690 bytes |
| コンパイル時間 | 477 ms |
| コンパイル使用メモリ | 128,044 KB |
| 最終ジャッジ日時 | 2024-06-22 16:35:25 |
| 合計ジャッジ時間 | 2,148 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(1469): Error: cannot modify `const` expression `target` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(1265): Error: template instance `std.algorithm.mutation.moveEmplaceImpl!(const(long))` error instantiating /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(1190): instantiated from here: `moveImpl!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(223): instantiated from here: `move!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(761): instantiated from here: `createNode!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(461): instantiated from here: `insertBeforeNode!(const(long))` Main.d(41): instantiated from here: `insertBack!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(1267): Error: template instance `std.algorithm.mutation.trustedMoveImpl!(const(long))` error instantiating /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/algorithm/mutation.d(1190): instantiated from here: `moveImpl!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(223): instantiated from here: `move!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(761): instantiated from here: `createNode!(const(long))` /home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/std/container/dlist.d(461): instantiated from here: `insertBeforeNode!(const(long))` Main.d(41): instantiated from here: `insertBack!(const(long))`
ソースコード
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) {
}
}