結果

問題 No.705 ゴミ拾い Hard
ユーザー 0w10w1
提出日時 2018-10-19 00:46:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 1,500 ms
コード長 2,080 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 213,184 KB
実行使用メモリ 10,264 KB
最終ジャッジ日時 2023-08-07 13:58:58
合計ジャッジ時間 8,964 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 160 ms
10,256 KB
testcase_25 AC 159 ms
10,112 KB
testcase_26 AC 160 ms
10,100 KB
testcase_27 AC 160 ms
10,108 KB
testcase_28 AC 159 ms
10,196 KB
testcase_29 AC 159 ms
10,108 KB
testcase_30 AC 164 ms
10,240 KB
testcase_31 AC 148 ms
10,184 KB
testcase_32 AC 147 ms
10,192 KB
testcase_33 AC 126 ms
10,244 KB
testcase_34 AC 125 ms
10,088 KB
testcase_35 AC 135 ms
10,256 KB
testcase_36 AC 143 ms
10,124 KB
testcase_37 AC 134 ms
10,264 KB
testcase_38 AC 142 ms
10,180 KB
testcase_39 AC 162 ms
10,184 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 156 ms
10,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename R> // return_type
struct MongeDP {
  vector<R> dp;
  vector<int> pre;
  function<bool(R, R)> cmp; // true is left better
  function<R(const vector<R> &dp, int, int)> get_cost;
  MongeDP(int n, function<bool(R, R)> c, function<R(const vector<R> &dp, int, int)> gc)
      : dp(n + 1), pre(n + 1, -1), cmp(c), get_cost(gc) {
    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
      dp[i] = get_cost(dp, pre[i] = get<0>(dcs.front()), 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(get_cost(dp, x, lb), get_cost(dp, 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(get_cost(dp, i, mb), get_cost(dp, 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);
      }
    }
  }
  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];

  auto abscub = [](int64_t x) { return abs(x * x * x); };

  MongeDP<int64_t> mdp(N, [](int64_t x, int64_t y) { return x < y; },
                       [&](const vector<int64_t> &dp, int x, int rb) {
                         return dp[x] + abscub(A[rb - 1] - X[x]) + abscub(Y[x]);
                       });

  cout << mdp[N] << endl;

  return 0;
}
0