結果

問題 No.1467 Selling Cars
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-04-03 23:18:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,394 ms / 4,000 ms
コード長 4,782 bytes
コンパイル時間 979 ms
コンパイル使用メモリ 120,620 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2023-09-04 12:30:46
合計ジャッジ時間 29,402 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,032 ms
5,984 KB
testcase_01 AC 1,394 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1,273 ms
4,380 KB
testcase_04 AC 1,270 ms
4,380 KB
testcase_05 AC 1,265 ms
4,376 KB
testcase_06 AC 1,269 ms
4,380 KB
testcase_07 AC 1,272 ms
4,376 KB
testcase_08 AC 1,271 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 823 ms
4,504 KB
testcase_12 AC 1,005 ms
4,452 KB
testcase_13 AC 942 ms
4,380 KB
testcase_14 AC 136 ms
4,380 KB
testcase_15 AC 306 ms
4,384 KB
testcase_16 AC 276 ms
6,016 KB
testcase_17 AC 744 ms
4,380 KB
testcase_18 AC 949 ms
4,380 KB
testcase_19 AC 1,268 ms
4,380 KB
testcase_20 AC 1,197 ms
4,376 KB
testcase_21 AC 1,195 ms
4,380 KB
testcase_22 AC 1,152 ms
4,488 KB
testcase_23 AC 1,184 ms
4,504 KB
testcase_24 AC 1,187 ms
4,380 KB
testcase_25 AC 48 ms
4,380 KB
testcase_26 AC 469 ms
4,376 KB
testcase_27 AC 465 ms
4,380 KB
testcase_28 AC 681 ms
4,376 KB
testcase_29 AC 152 ms
4,376 KB
testcase_30 AC 183 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 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