結果

問題 No.704 ゴミ拾い Medium
ユーザー miscalcmiscalc
提出日時 2022-08-27 18:24:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 381 ms / 1,500 ms
コード長 5,651 bytes
コンパイル時間 4,649 ms
コンパイル使用メモリ 274,040 KB
実行使用メモリ 19,184 KB
最終ジャッジ日時 2024-10-14 15:14:51
合計ジャッジ時間 13,668 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 2 ms
6,824 KB
testcase_14 AC 4 ms
6,820 KB
testcase_15 AC 4 ms
6,816 KB
testcase_16 AC 3 ms
6,816 KB
testcase_17 AC 3 ms
6,816 KB
testcase_18 AC 3 ms
6,820 KB
testcase_19 AC 4 ms
6,820 KB
testcase_20 AC 3 ms
6,816 KB
testcase_21 AC 3 ms
6,816 KB
testcase_22 AC 3 ms
6,816 KB
testcase_23 AC 4 ms
6,820 KB
testcase_24 AC 376 ms
19,024 KB
testcase_25 AC 378 ms
19,136 KB
testcase_26 AC 376 ms
19,064 KB
testcase_27 AC 377 ms
19,100 KB
testcase_28 AC 375 ms
19,144 KB
testcase_29 AC 377 ms
19,184 KB
testcase_30 AC 377 ms
19,064 KB
testcase_31 AC 381 ms
19,068 KB
testcase_32 AC 377 ms
19,092 KB
testcase_33 AC 377 ms
19,028 KB
testcase_34 AC 235 ms
14,888 KB
testcase_35 AC 231 ms
15,008 KB
testcase_36 AC 231 ms
14,948 KB
testcase_37 AC 229 ms
14,976 KB
testcase_38 AC 230 ms
14,868 KB
testcase_39 AC 230 ms
15,052 KB
testcase_40 AC 231 ms
14,948 KB
testcase_41 AC 230 ms
14,896 KB
testcase_42 AC 231 ms
15,104 KB
testcase_43 AC 229 ms
14,908 KB
testcase_44 AC 2 ms
6,820 KB
testcase_45 AC 2 ms
6,820 KB
testcase_46 AC 291 ms
15,104 KB
testcase_47 AC 284 ms
15,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll safemod(ll A, ll M) {ll res = A % M; if (res < 0) res += M; return res;}
ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;}
ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);}
ll pow_ll(ll A, ll B) {if (A == 0 || A == 1) {return A;} if (A == -1) {return B & 1 ? -1 : 1;} ll res = 1; for (int i = 0; i < B; i++) {res *= A;} return res;}
ll logfloor(ll A, ll B) {assert(A >= 2); ll res = 0; for (ll tmp = 1; tmp <= B / A; tmp *= A) {res++;} return res;}
ll logceil(ll A, ll B) {assert(A >= 2); ll res = 0; for (ll tmp = 1; tmp < B; tmp *= A) {res++;} return res;}
ll arisum_ll(ll a, ll d, ll n) { return n * a + (n & 1 ? ((n - 1) >> 1) * n : (n >> 1) * (n - 1)) * d; }
ll arisum2_ll(ll a, ll l, ll n) { return n & 1 ? ((a + l) >> 1) * n : (n >> 1) * (a + l); }
ll arisum3_ll(ll a, ll l, ll d) { assert((l - a) % d == 0); return arisum2_ll(a, l, (l - a) / d + 1); }
template<class T> void unique(vector<T> &V) {V.erase(unique(V.begin(), V.end()), V.end());}
template<class T> void sortunique(vector<T> &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
template<class T> void printvec(const vector<T> &V) {int _n = V.size(); for (int i = 0; i < _n; i++) cout << V[i] << (i == _n - 1 ? "" : " ");cout << '\n';}
template<class T> void printvect(const vector<T> &V) {for (auto v : V) cout << v << '\n';}
template<class T> void printvec2(const vector<vector<T>> &V) {for (auto &v : V) printvec(v);}
//*
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;
//*/

template<class T>
struct LiChaoTree
{
private:
  int n;
  vector<T> X;
  vector<pair<T, T>> node;
  int type;

  void add_line_internal(T a, T b, int i, int l, int r)
  {
    auto [a0, b0] = node[i];
    bool small_l = a * X[l] + b <= a0 * X[l] + b0;
    bool small_r = a * X[r - 1] + b <= a0 * X[r - 1] + b0;
    if (!small_l && !small_r)
      return;
    if (small_l && small_r)
    {
      node[i] = make_pair(a, b);
      return;
    }
    if (i >= n)
      return;
    int m = (l + r) / 2;
    bool small_m = a * X[m] + b <= a0 * X[m] + b0;
    if (!small_m)
    {
      if (small_l)
        add_line_internal(a, b, 2 * i, l, m);
      else if (small_r)
        add_line_internal(a, b, 2 * i + 1, m, r);
      else
        assert(false);
    }
    else
    {
      node[i] = make_pair(a, b);
      if (!small_l)
        add_line_internal(a0, b0, 2 * i, l, m);
      else if (!small_r)
        add_line_internal(a0, b0, 2 * i + 1, m, r);
      else
        assert(false);
    }
  }

  void add_segment_internal(T a, T b, T lx, T rx, int i, int l, int r)
  {
    if (X[r - 1] < lx || rx <= X[l])
      return;
    if (lx <= X[l] && X[r - 1] < rx)
    {
      add_line_internal(a, b, i, l, r);
      return;
    }
    if (r - l <= 1)
      return;
    int m = (l + r) / 2;
    add_segment_internal(a, b, lx, rx, 2 * i, l, m);
    add_segment_internal(a, b, lx, rx, 2 * i + 1, m, r);
  }

public:
  LiChaoTree(const vector<T> &xs, bool sorted_unique = false) // クエリで問われる x の値を格納した配列を渡す
  {
    type = 0;
    X = vector<T>(xs);
    if (!sorted_unique)
      sortunique(X);
    n = 1;
    while (n < (int)X.size())
      n <<= 1;
    X.resize(2 * n, X.back());
    node.assign(2 * n, make_pair(0, INF));
  }

  LiChaoTree(T x_min, T x_max) // 上の xs = {x_min, …, x_max} と同じ(get_min が少し早くなる)
  {
    type = 1;
    X.resize(x_max - x_min + 1);
    for (int i = 0; i <= x_max - x_min; i++)
      X[i] = x_min + i;
    n = 1;
    while (n < (int)X.size())
      n <<= 1;
    X.resize(2 * n, X.back());
    node.assign(2 * n, make_pair(0, INF));
  }

  void add_line(T a, T b) { add_line_internal(a, b, 1, 0, n); }

  void add_segment(T a, T b, T lx, T rx) { add_segment_internal(a, b, lx, rx, 1, 0, n); }

  T get_min(T p)
  {
    int i;
    if (type == 0)
    {
      i = lower_bound(X.begin(), X.end(), p) - X.begin();
      assert(X.at(i) == p);
    }
    else if (type == 1)
    {
      assert(X.front() <= p && p <= X.back());
      i = p - X.front();
    }
    else
      assert(false);
    i += n;

    T res = INF;
    while (i > 0)
    {
      auto [a, b] = node[i];
      chmin(res, a * p + b);
      i >>= 1;
    }
    return res;
  }
};

int main()
{
  ll N;
  cin >> N;
  vector<ll> A(N), X(N), Y(N);
  for (ll i = 0; i < N; i++)
  {
    cin >> A.at(i);
  }
  for (ll i = 0; i < N; i++)
  {
    cin >> X.at(i);
  }
  for (ll i = 0; i < N; i++)
  {
    cin >> Y.at(i);
  }

  LiChaoTree<ll> cht(A);

  /*
  vector<ll> dp(N + 1, INF);
  dp.at(0) = 0;
  for (ll i = 0; i < N; i++)
  {
    for (ll j = 0; j <= i; j++)
    {
      ll tmp = dp.at(j) + abs(X.at(j) - A.at(i)) + Y.at(j);
      chmin(dp.at(i + 1), tmp);
    }
  }
  ll ans = dp.at(N);
  cout << ans << endl;
  //*/

  //*
  vector<ll> dp(N + 1, INF);
  dp.at(0) = 0;
  for (ll i = 0; i < N; i++)
  {
    cht.add_segment(-1, X.at(i) + Y.at(i) + dp.at(i), -INF, X.at(i));
    cht.add_segment(1, -X.at(i) + Y.at(i) + dp.at(i), X.at(i), INF);
    dp.at(i + 1) = cht.get_min(A.at(i));
  }
  ll ans = dp.at(N);
  cout << ans << endl;
  //*/
}
0