結果

問題 No.2114 01 Matching
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-11-01 12:23:32
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,695 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 124,104 KB
実行使用メモリ 61,308 KB
最終ジャッジ日時 2023-09-04 18:47:44
合計ジャッジ時間 21,501 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 240 ms
38,044 KB
testcase_03 AC 65 ms
16,404 KB
testcase_04 AC 90 ms
18,720 KB
testcase_05 WA -
testcase_06 AC 257 ms
42,836 KB
testcase_07 AC 186 ms
28,280 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 213 ms
34,128 KB
testcase_11 AC 333 ms
60,872 KB
testcase_12 AC 331 ms
61,308 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 223 ms
37,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 328 ms
60,248 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 331 ms
59,664 KB
testcase_27 AC 330 ms
59,848 KB
testcase_28 AC 169 ms
34,648 KB
testcase_29 AC 329 ms
60,844 KB
testcase_30 AC 188 ms
31,060 KB
testcase_31 AC 332 ms
60,828 KB
testcase_32 WA -
testcase_33 AC 239 ms
39,484 KB
testcase_34 AC 145 ms
29,348 KB
testcase_35 AC 123 ms
22,580 KB
testcase_36 AC 334 ms
59,872 KB
testcase_37 WA -
testcase_38 AC 189 ms
27,488 KB
testcase_39 WA -
testcase_40 AC 123 ms
33,404 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 260 ms
42,856 KB
testcase_44 WA -
testcase_45 AC 288 ms
59,664 KB
testcase_46 WA -
testcase_47 AC 330 ms
59,644 KB
testcase_48 AC 330 ms
60,752 KB
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 186 ms
29,100 KB
testcase_52 AC 222 ms
46,396 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)); }


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