結果

問題 No.704 ゴミ拾い Medium
ユーザー ei1333333ei1333333
提出日時 2018-06-15 23:09:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,257 bytes
コンパイル時間 2,042 ms
コンパイル使用メモリ 203,680 KB
実行使用メモリ 26,752 KB
最終ジャッジ日時 2023-09-13 05:10:27
合計ジャッジ時間 9,287 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,476 KB
testcase_01 AC 2 ms
7,508 KB
testcase_02 AC 2 ms
7,628 KB
testcase_03 AC 2 ms
7,576 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 3 ms
7,536 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
7,516 KB
testcase_12 WA -
testcase_13 AC 3 ms
7,772 KB
testcase_14 WA -
testcase_15 AC 3 ms
7,552 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3 ms
7,540 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 2 ms
7,628 KB
testcase_45 AC 2 ms
7,512 KB
testcase_46 WA -
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 58;

template< typename Monoid >
struct SegmentTree {
  using F = function< Monoid(Monoid, Monoid) >;

  int sz;
  vector< Monoid > seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) {
    seg[k + sz] = x;
  }

  void build() {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const {
    return seg[k + sz];
  }
};


int main() {
  int64 N, A[300001];
  int64 X[300001], Y[300001];

  scanf("%lld", &N);
  for(int i = 0; i < N; i++) scanf("%lld", &A[i]);
  for(int i = 0; i < N; i++) scanf("%lld", &X[i]);
  for(int i = 0; i < N; i++) scanf("%lld", &Y[i]);

  /*
  vector< int64 > dp(N + 1, INF);
  dp[0] = 0;
  for(int i = 1; i <= N; i++) {
    int64 ret = INF;
    for(int j = i; j >= 0; j--) {
      int64 dist = dp[j] + abs(A[i - 1] - X[j]) + Y[j];
      ret = min(ret, dist);
    }
    dp[i] = ret;
  }
  cout << dp[N] << endl;
*/

  /* 実 家 の よ う な 安 心 感 */
  /* ア デ ィ ダ ス の セ グ 木 */

  auto f = [&](int64 x, int64 y) { return min(x, y); };
  SegmentTree< int64 > latte(N + 1, f, INF), malta(N + 1, f, INF);

  auto update = [&](int idx, int64 v) {
    auto it = lower_bound(A, A + N, X[idx]) - A;
    latte.update(it, v + Y[idx] + X[idx]);
    malta.update(it, v + Y[idx] - X[idx]);
  };

  update(0, 0);

  for(int i = 1; i <= N; i++) {
    int64 ret = min(latte.query(i, N + 1) - A[i - 1], malta.query(0, i) + A[i - 1]);
    // dp[i] = (dp[j]+Y[j]+X[j])-A[i-1]  (A[i-1]≦X[j])
    //         (dp[j]+Y[j]-X[j])+A[i-1]  (A[i-1]≧X[j])
    if(i == N) cout << ret << endl;
    else update(i, ret);
  }
}
0