結果

問題 No.703 ゴミ拾い Easy
ユーザー te-shte-sh
提出日時 2018-07-05 16:57:51
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 92 ms / 1,500 ms
コード長 1,252 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 93,868 KB
実行使用メモリ 27,296 KB
最終ジャッジ日時 2023-09-03 20:36:49
合計ジャッジ時間 9,052 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 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,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 88 ms
23,832 KB
testcase_25 AC 88 ms
23,668 KB
testcase_26 AC 89 ms
25,036 KB
testcase_27 AC 89 ms
24,260 KB
testcase_28 AC 88 ms
23,648 KB
testcase_29 AC 88 ms
24,228 KB
testcase_30 AC 87 ms
23,888 KB
testcase_31 AC 88 ms
23,868 KB
testcase_32 AC 88 ms
24,072 KB
testcase_33 AC 87 ms
23,704 KB
testcase_34 AC 75 ms
21,992 KB
testcase_35 AC 75 ms
23,280 KB
testcase_36 AC 75 ms
23,204 KB
testcase_37 AC 74 ms
22,668 KB
testcase_38 AC 74 ms
22,968 KB
testcase_39 AC 74 ms
22,736 KB
testcase_40 AC 73 ms
22,604 KB
testcase_41 AC 76 ms
23,288 KB
testcase_42 AC 75 ms
23,228 KB
testcase_43 AC 75 ms
23,996 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 92 ms
27,296 KB
testcase_47 AC 91 ms
26,428 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,376 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);}

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), cht = ConvexHullTrickSimple!long(n);

  foreach (i; 0..n) {
    cht.add(-2*x[i], dp[i]+x[i]^^2+y[i]^^2);
    dp[i+1] = cht.get(a[i]) + a[i]^^2;
  }

  writeln(dp[n]);
}

struct ConvexHullTrickSimple(T)
{
  import std.typecons;

  alias Line = Tuple!(T, T);
  Line[] deq;
  int s = 0, t = 0;

  this(size_t n)
  {
    deq = new Line[](n);
  }

  auto add(T a, T b)
  {
    auto p = Line(a, b);
    while (s+1 < t && check(deq[t-2], deq[t-1], p)) --t;
    deq[t++] = p;
  }

  auto get(T x)
  {
    while (s+1 < t && f(deq[s], x) >= f(deq[s+1], x)) ++s;
    return f(deq[s], x);
  }

  auto check(Line p1, Line p2, Line p3)
  {
    return (p2[0]-p1[0])*(p3[1]-p2[1]) >= (p2[1]-p1[1])*(p3[0]-p2[0]);
  }

  auto f(Line p, T x)
  {
    return p[0]*x+p[1];
  }
}
0