結果

問題 No.704 ゴミ拾い Medium
ユーザー te-shte-sh
提出日時 2018-07-05 17:45:41
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 185 ms / 1,500 ms
コード長 1,853 bytes
コンパイル時間 600 ms
コンパイル使用メモリ 94,308 KB
実行使用メモリ 40,888 KB
最終ジャッジ日時 2023-09-03 20:37:21
合計ジャッジ時間 6,496 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 154 ms
37,412 KB
testcase_25 AC 185 ms
37,228 KB
testcase_26 AC 164 ms
37,156 KB
testcase_27 AC 161 ms
38,020 KB
testcase_28 AC 173 ms
37,484 KB
testcase_29 AC 151 ms
39,188 KB
testcase_30 AC 165 ms
38,756 KB
testcase_31 AC 176 ms
36,440 KB
testcase_32 AC 158 ms
38,536 KB
testcase_33 AC 163 ms
36,724 KB
testcase_34 AC 143 ms
36,620 KB
testcase_35 AC 143 ms
36,648 KB
testcase_36 AC 143 ms
35,516 KB
testcase_37 AC 143 ms
35,520 KB
testcase_38 AC 145 ms
36,544 KB
testcase_39 AC 144 ms
36,824 KB
testcase_40 AC 147 ms
36,748 KB
testcase_41 AC 145 ms
35,352 KB
testcase_42 AC 144 ms
34,928 KB
testcase_43 AC 143 ms
35,144 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 150 ms
37,752 KB
testcase_47 AC 159 ms
40,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readA(T)(size_t n,ref T[]t){t=new T[](n);auto r=rdsp;foreach(ref v;t)pick(r,v);}

const inf = 10L^^18;
alias SegTree = SegmentTree!(long, min);

void main()
{
  int n; readV(n);
  long[] a; readA(n, a);
  long[] x; readA(n, x);
  long[] y; readA(n, y);

  auto dp = new long[](n+1), st1 = new SegTree(n, inf), st2 = new SegTree(n, inf);
  foreach (i; 0..n) {
    st1[i] = dp[i]+y[i]-x[i];
    st2[i] = dp[i]+y[i]+x[i];

    auto p = x[0..i+1].assumeSorted.lowerBound(a[i]).length;
    dp[i+1] = min(st1[0..p]+a[i], st2[p..i+1]-a[i]);
  }

  writeln(dp[n]);
}

class SegmentTree(T, alias pred = "a + b")
{
  import core.bitop, std.functional;
  alias predFun = binaryFun!pred;

  const size_t n, an;
  T[] buf;
  T unit;

  this(size_t n, T unit = T.init)
  {
    this.n = n;
    this.unit = unit;
    an = n == 1 ? 1 : (1 << ((n-1).bsr + 1));
    buf = new T[](an*2);
    if (T.init != unit) buf[] = unit;
  }

  this(T[] init, T unit = T.init)
  {
    this(init.length, unit);
    buf[an..an+n][] = init[];
    foreach_reverse (i; 1..an)
      buf[i] = predFun(buf[i*2], buf[i*2+1]);
  }

  void opIndexAssign(T val, size_t i)
  {
    buf[i += an] = val;
    while (i /= 2)
      buf[i] = predFun(buf[i*2], buf[i*2+1]);
  }

  pure T opSlice(size_t l, size_t r)
  {
    l += an; r += an;
    T r1 = unit, r2 = unit;
    while (l != r) {
      if (l % 2) r1 = predFun(r1, buf[l++]);
      if (r % 2) r2 = predFun(buf[--r], r2);
      l /= 2; r /= 2;
    }
    return predFun(r1, r2);
  }

  pure T opIndex(size_t i) { return buf[i+an]; }
  pure size_t opDollar() { return n; }
}
0