結果

問題 No.1467 Selling Cars
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-04-03 23:18:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,347 ms / 4,000 ms
コード長 4,782 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 136,480 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 11:10:18
合計ジャッジ時間 27,682 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,001 ms
6,816 KB
testcase_01 AC 1,347 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1,229 ms
6,940 KB
testcase_04 AC 1,230 ms
6,940 KB
testcase_05 AC 1,250 ms
6,940 KB
testcase_06 AC 1,240 ms
6,944 KB
testcase_07 AC 1,235 ms
6,940 KB
testcase_08 AC 1,234 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 799 ms
6,940 KB
testcase_12 AC 975 ms
6,940 KB
testcase_13 AC 918 ms
6,944 KB
testcase_14 AC 130 ms
6,940 KB
testcase_15 AC 292 ms
6,944 KB
testcase_16 AC 269 ms
6,940 KB
testcase_17 AC 718 ms
6,944 KB
testcase_18 AC 929 ms
6,940 KB
testcase_19 AC 1,224 ms
6,944 KB
testcase_20 AC 1,152 ms
6,944 KB
testcase_21 AC 1,153 ms
6,944 KB
testcase_22 AC 1,111 ms
6,940 KB
testcase_23 AC 1,140 ms
6,940 KB
testcase_24 AC 1,146 ms
6,944 KB
testcase_25 AC 46 ms
6,940 KB
testcase_26 AC 450 ms
6,940 KB
testcase_27 AC 452 ms
6,944 KB
testcase_28 AC 647 ms
6,944 KB
testcase_29 AC 146 ms
6,940 KB
testcase_30 AC 175 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 1 ms
6,940 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)); }


enum INF = 10L^^18;
enum BIG = 10L^^14;

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      auto A = new long[M];
      foreach (i; 0 .. M) {
        A[i] = readLong();
      }
      auto B = new long[N];
      foreach (j; 0 .. N) {
        B[j] = readLong();
      }
      
      auto xs = (A ~ B).sort.uniq.array;
      const xsLen = cast(int)(xs.length);
      debug {
        writeln("A = ", A);
        writeln("B = ", B);
        writeln("xsLen = ", xsLen);
      }
      
      auto as = new int[M];
      foreach (i; 0 .. M) {
        as[i] = xs.lowerBound(A[i]);
      }
      auto bs = new int[N];
      foreach (j; 0 .. N) {
        bs[j] = xs.lowerBound(B[j]);
      }
      
      foreach (k; 1 .. M + 1) {
        debug {
          writefln("-------- k = %s --------", k);
        }
        auto ss = new long[xsLen], ts = new long[xsLen];
        foreach (i; 0 .. M) {
          ss[as[i]] += 1;
        }
        foreach (j; 0 .. N) {
          ts[bs[j]] += k;
        }
        
        alias Entry = Tuple!(long, "slope", long, "len");
        DList!Entry queL, queR;
        long ofsL, ofsR;
        long lenR;
        long val;
        queL.insertBack(Entry(-BIG, INF));
        foreach (e; 0 .. xsLen) {
          if (ss[e] > 0) {
            debug {
              writefln("shift +%s", ss[e]);
            }
            for (long need = ss[e]; need > 0; ) {
              const tmp = min(need, queL.back.len);
              queR.insertFront(Entry(queL.back.slope + ofsL - ofsR, tmp));
              need -= tmp;
              if ((queL.back.len -= tmp) == 0) {
                queL.removeBack;
              }
              lenR += tmp;
            }
          }
          if (ts[e] > 0) {
            debug {
              writefln("stretch min -%s", ts[e]);
            }
            queR.insertBack(Entry(0 - ofsR, ts[e]));
            for (long need = ts[e]; need > 0; ) {
              const tmp = min(need, queR.front.len);
              queL.insertBack(Entry(queR.front.slope + ofsR - ofsL, tmp));
              need -= tmp;
              if ((queR.front.len -= tmp) == 0) {
                queR.removeFront;
              }
            }
          }
          debug {
            write("  L:");
            foreach (ref entry; queL) writef(" (%s, %s)", entry.slope + ofsL, entry.len);
            writeln;
            write("  R:");
            foreach (ref entry; queR) writef(" (%s, %s)", entry.slope + ofsR, entry.len);
            writeln;
            writefln("  lenR = %s, val = %s", lenR, val);
          }
          if (e + 1 < xsLen) {
            const x = xs[e + 1] - xs[e];
            debug {
              writefln("add %s |t|", x);
            }
            ofsL -= x;
            ofsR += x;
            val += x * lenR;
            for (; !queR.empty && queR.back.slope + ofsR > 0; ) {
              lenR -= queR.back.len;
              val -= (queR.back.slope + ofsR) * queR.back.len;
              queR.removeBack;
            }
          }
          debug {
            write("  L:");
            foreach (ref entry; queL) writef(" (%s, %s)", entry.slope + ofsL, entry.len);
            writeln;
            write("  R:");
            foreach (ref entry; queR) writef(" (%s, %s)", entry.slope + ofsR, entry.len);
            writeln;
            writefln("  lenR = %s, val = %s", lenR, val);
          }
        }
        long ans = val;
        foreach_reverse (ref entry; queR) {
          ans -= (entry.slope + ofsR) * entry.len;
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0