結果

問題 No.703 ゴミ拾い Easy
ユーザー 0w10w1
提出日時 2018-10-19 00:41:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 1,500 ms
コード長 2,062 bytes
コンパイル時間 4,632 ms
コンパイル使用メモリ 212,376 KB
実行使用メモリ 10,284 KB
最終ジャッジ日時 2023-08-30 19:02:36
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 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,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 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 151 ms
10,112 KB
testcase_25 AC 149 ms
10,084 KB
testcase_26 AC 152 ms
10,284 KB
testcase_27 AC 151 ms
10,100 KB
testcase_28 AC 151 ms
10,188 KB
testcase_29 AC 149 ms
10,192 KB
testcase_30 AC 151 ms
10,120 KB
testcase_31 AC 150 ms
10,156 KB
testcase_32 AC 151 ms
10,196 KB
testcase_33 AC 150 ms
10,120 KB
testcase_34 AC 138 ms
10,172 KB
testcase_35 AC 138 ms
10,188 KB
testcase_36 AC 138 ms
10,072 KB
testcase_37 AC 137 ms
10,036 KB
testcase_38 AC 137 ms
10,112 KB
testcase_39 AC 137 ms
9,988 KB
testcase_40 AC 138 ms
10,068 KB
testcase_41 AC 136 ms
10,124 KB
testcase_42 AC 136 ms
10,100 KB
testcase_43 AC 138 ms
10,092 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 151 ms
10,064 KB
testcase_47 AC 151 ms
10,104 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 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 sqr = [](int64_t x) { return 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] + sqr(A[rb - 1] - X[x]) + sqr(Y[x]);
                       });

  cout << mdp[N] << endl;

  return 0;
}
0