結果

問題 No.704 ゴミ拾い Medium
ユーザー miscalcmiscalc
提出日時 2022-08-27 18:22:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,600 bytes
コンパイル時間 5,046 ms
コンパイル使用メモリ 264,704 KB
実行使用メモリ 24,576 KB
最終ジャッジ日時 2024-04-22 16:06:09
合計ジャッジ時間 10,627 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 (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