結果

問題 No.409 ダイエット
ユーザー 0w10w1
提出日時 2019-10-17 14:22:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 2,421 bytes
コンパイル時間 2,692 ms
コンパイル使用メモリ 213,644 KB
実行使用メモリ 7,676 KB
最終ジャッジ日時 2023-09-06 15:54:15
合計ジャッジ時間 8,795 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 1 ms
4,384 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,384 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,384 KB
testcase_37 AC 2 ms
4,384 KB
testcase_38 AC 2 ms
4,384 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 3 ms
4,384 KB
testcase_41 AC 2 ms
4,384 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 2 ms
4,384 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 3 ms
4,384 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,384 KB
testcase_49 AC 2 ms
4,384 KB
testcase_50 AC 3 ms
4,384 KB
testcase_51 AC 2 ms
4,384 KB
testcase_52 AC 3 ms
4,384 KB
testcase_53 AC 2 ms
4,384 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 43 ms
5,208 KB
testcase_56 AC 84 ms
7,344 KB
testcase_57 AC 91 ms
7,448 KB
testcase_58 AC 38 ms
5,008 KB
testcase_59 AC 57 ms
5,912 KB
testcase_60 AC 34 ms
4,688 KB
testcase_61 AC 78 ms
7,036 KB
testcase_62 AC 82 ms
7,440 KB
testcase_63 AC 81 ms
6,960 KB
testcase_64 AC 42 ms
4,912 KB
testcase_65 AC 91 ms
7,064 KB
testcase_66 AC 91 ms
7,348 KB
testcase_67 AC 67 ms
6,612 KB
testcase_68 AC 57 ms
5,676 KB
testcase_69 AC 80 ms
6,808 KB
testcase_70 AC 79 ms
6,632 KB
testcase_71 AC 44 ms
4,884 KB
testcase_72 AC 99 ms
7,676 KB
testcase_73 AC 96 ms
7,080 KB
testcase_74 AC 54 ms
5,864 KB
testcase_75 AC 91 ms
7,060 KB
testcase_76 AC 62 ms
6,084 KB
testcase_77 AC 45 ms
5,116 KB
testcase_78 AC 46 ms
5,228 KB
testcase_79 AC 38 ms
4,924 KB
testcase_80 AC 89 ms
7,168 KB
testcase_81 AC 92 ms
6,996 KB
testcase_82 AC 65 ms
6,256 KB
testcase_83 AC 71 ms
6,564 KB
testcase_84 AC 61 ms
5,720 KB
testcase_85 AC 8 ms
4,380 KB
testcase_86 AC 59 ms
5,864 KB
testcase_87 AC 81 ms
6,608 KB
testcase_88 AC 33 ms
4,572 KB
testcase_89 AC 78 ms
5,960 KB
testcase_90 AC 33 ms
4,384 KB
testcase_91 AC 85 ms
6,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename R> // return_type
struct MongeDP {
  int n;
  vector<R> dp;
  vector<int> pre;
  function<bool(R, R)> cmp; // true is left better
  function<R(int, int)> w;
  MongeDP(int _n, function<bool(R, R)> c, function<R(int, int)> get_cost)
      : n(_n), dp(n + 1), pre(n + 1, -1), cmp(c), w(get_cost) {
    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
      pre[i] = get<0>(dcs.front());
      dp[i] = dp[pre[i]] + w(pre[i], 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(dp[x] + w(x, lb), dp[i] + w(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(dp[i] + w(i, mb), dp[x] + w(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);
      }
    }
  }
  void ensure_monge_condition() {
    // Monge Condition: i <= j <= k <= l then w(i, l) + w(j, k) >(<)= w(i, k) + w(j, l)
    for (int i = 0; i <= n; ++i)
      for (int j = i; j <= n; ++j)
        for (int k = j; k <= n; ++k)
          for (int l = k; l <= n; ++l) {
            R w0 = w(i, l), w1 = w(j, k), w2 = w(i, k), w3 = w(j, l);
            assert(w0 + w1 >= w2 + w3); // if maximization, revert the sign
          }
  }
  R operator[](int x) { return dp[x]; }
};

signed main() {
  ios::sync_with_stdio(false);
  
  int N, A, B, W;
  cin >> N >> A >> B >> W;

  vector<int> D(N);
  for (int i = 0; i < N; ++i) {
    cin >> D[i];
  }

  D.push_back(-A);
  ++N;

  MongeDP<int64_t> mdp(N, [](int64_t x, int64_t y) { return x < y; },
                       [&](int x, int rb) {
                         int i = rb - 1 - x; 
                         return D[rb - 1] - (int64_t) i * A + (int64_t) i * (i + 1) / 2 * B;
                       });

  int64_t ans = W + A + mdp[N];
  cout << ans << endl;

  return 0;
}
0