結果

問題 No.704 ゴミ拾い Medium
ユーザー 0w10w1
提出日時 2018-10-19 03:01:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 1,500 ms
コード長 2,456 bytes
コンパイル時間 2,272 ms
コンパイル使用メモリ 209,192 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2024-04-25 14:48:10
合計ジャッジ時間 9,101 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,948 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 160 ms
10,240 KB
testcase_25 AC 159 ms
10,368 KB
testcase_26 AC 156 ms
10,240 KB
testcase_27 AC 149 ms
10,112 KB
testcase_28 AC 150 ms
10,368 KB
testcase_29 AC 147 ms
10,276 KB
testcase_30 AC 153 ms
10,240 KB
testcase_31 AC 153 ms
10,240 KB
testcase_32 AC 153 ms
10,368 KB
testcase_33 AC 147 ms
10,240 KB
testcase_34 AC 140 ms
10,240 KB
testcase_35 AC 137 ms
10,112 KB
testcase_36 AC 141 ms
10,240 KB
testcase_37 AC 142 ms
10,240 KB
testcase_38 AC 139 ms
10,240 KB
testcase_39 AC 136 ms
10,112 KB
testcase_40 AC 136 ms
10,212 KB
testcase_41 AC 136 ms
10,368 KB
testcase_42 AC 138 ms
10,112 KB
testcase_43 AC 144 ms
10,240 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 2 ms
6,940 KB
testcase_46 AC 163 ms
10,368 KB
testcase_47 AC 155 ms
10,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename R> // return_type
struct MongeDP {
  int n;
  vector<R> dp;
  vector<int> pre;
  function<bool(R, R)> cmp; // true is left better
  function<R(int, int)> w; // w(i, j) = cost(dp[i] -> dp[j])
  MongeDP(int _n, function<bool(R, R)> c, function<R(int, int)> get_cost)
      : n(_n), dp(n + 1), pre(n + 1, -1), cmp(c), w(get_cost) {
    deque<tuple<int, int, int>> dcs; // decision
    dcs.emplace_back(0, 1, n); // transition from dp[0] is effective for [1, N]
    for (int i = 1; i <= n; ++i) {
      while (get<2>(dcs.front()) < i) dcs.pop_front(); // right bound is out-dated
      pre[i] = get<0>(dcs.front());
      dp[i] = dp[pre[i]] + w(pre[i], i); // best t is A[dcs.top(), i)
      while (dcs.size()) {
        int x, lb, rb;
        tie(x, lb, rb) = dcs.back();
        if (lb <= i) break; // will be pop_fronted soon anyway
        if (!cmp(dp[x] + w(x, lb), dp[i] + w(i, lb))) {
          dcs.pop_back();
          if (dcs.size()) get<2>(dcs.back()) = n;
        } else break;
      }
      int best = -1;
      for (int lb = i + 1, rb = n, x = get<0>(dcs.back()); lb <= rb; ) {
        int mb = lb + rb >> 1;
        if (cmp(dp[i] + w(i, mb), dp[x] + w(x, mb))) {
          best = mb;
          rb = mb - 1;
        } else lb = mb + 1;
      }
      if (~best) {
        get<2>(dcs.back()) = best - 1;
        dcs.emplace_back(i, best, n);
      }
    }
  }
  void ensure_monge_condition() {
    // Monge Condition: i <= j <= k <= l then w(i, l) + w(j, k) >(<)= w(i, k) + w(j, l)
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        for (int k = j; k <= n; ++k)
          for (int l = k; l <= n; ++l) {
            R w0 = w(i, l), w1 = w(j, k), w2 = w(i, k), w3 = w(j, l);
            assert(w0 + w1 >= w2 + w3); // if maximization, revert the sign
          }
  }
  R operator[](int x) { return dp[x]; }
};

signed main() {
  ios::sync_with_stdio(false);
  
  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) cin >> A[i];

  vector<int> X(N);
  for (int i = 0; i < N; ++i) cin >> X[i];

  vector<int> Y(N);
  for (int i = 0; i < N; ++i) cin >> Y[i];

  MongeDP<int64_t> mdp(N, [](int64_t x, int64_t y) { return x < y; },
                       [&](int x, int rb) {
                         return abs(A[rb - 1] - X[x]) + abs(Y[x]);
                       });
  // mdp.ensure_monge_condition();

  cout << mdp[N] << endl;

  return 0;
}
0