結果

問題 No.860 買い物
ユーザー risujirohrisujiroh
提出日時 2019-08-09 22:25:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,405 bytes
コンパイル時間 1,670 ms
コンパイル使用メモリ 169,816 KB
実行使用メモリ 10,636 KB
最終ジャッジ日時 2023-09-26 18:26:56
合計ジャッジ時間 5,467 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,772 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 550 ms
4,380 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct SegmentTree {
  using T = int;
  static T op(const T& a, const T& b) { return min(a, b); }
  static constexpr T e() { return 2e9; }

  const int n;
  V<T> t;
  SegmentTree(int n) : n(n), t(2 * n, e()) {}
  T& operator[](int i) { return t[i + n]; }
  void build() { for (int i = n - 1; i; --i) t[i] = op(t[2 * i], t[2 * i + 1]); }
  T acc(int l, int r) const {
    T resl = e(), resr = e();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) resl = op(resl, t[l++]);
      if (r & 1) resr = op(t[--r], resr);
    }
    return op(resl, resr);
  }
  void set(int i, const T& a) {
    t[i += n] = a;
    while (i >>= 1) t[i] = op(t[2 * i], t[2 * i + 1]);
  }
};

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  V<lint> c(n + 1), d(n + 1); for (int i = 0; i < n; ++i) cin >> c[i] >> d[i];
  SegmentTree st(n);
  for (int i = n - 1; i >= 0; --i) {
    st[i] = c[i];
    c[i] += c[i + 1];
    d[i] += d[i + 1];
  }
  st.build();

  V<lint> dp(n + 1, 1e18);
  dp[n] = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = i + 1; j <= n; ++j) {
      lint curr = (c[i] - c[j]) + (d[i + 1] - d[j]) + st.acc(i, j);
      dp[i] = min(dp[i], curr + dp[j]);
    }
  }
  cout << dp[0] << '\n';
}
0