結果

問題 No.409 ダイエット
ユーザー miyo2580miyo2580
提出日時 2023-10-06 23:09:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 3,526 bytes
コンパイル時間 2,196 ms
コンパイル使用メモリ 204,108 KB
実行使用メモリ 19,932 KB
最終ジャッジ日時 2023-10-06 23:09:46
合計ジャッジ時間 10,726 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 3 ms
4,376 KB
testcase_46 AC 3 ms
4,376 KB
testcase_47 AC 3 ms
4,380 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 3 ms
4,376 KB
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 3 ms
4,380 KB
testcase_54 AC 3 ms
4,376 KB
testcase_55 AC 49 ms
11,028 KB
testcase_56 AC 26 ms
7,740 KB
testcase_57 AC 101 ms
19,864 KB
testcase_58 AC 44 ms
10,196 KB
testcase_59 AC 60 ms
11,152 KB
testcase_60 AC 39 ms
9,848 KB
testcase_61 AC 89 ms
17,652 KB
testcase_62 AC 93 ms
18,880 KB
testcase_63 AC 87 ms
16,704 KB
testcase_64 AC 46 ms
10,312 KB
testcase_65 AC 88 ms
14,072 KB
testcase_66 AC 98 ms
18,344 KB
testcase_67 AC 76 ms
15,780 KB
testcase_68 AC 60 ms
11,412 KB
testcase_69 AC 85 ms
16,304 KB
testcase_70 AC 88 ms
16,428 KB
testcase_71 AC 50 ms
10,880 KB
testcase_72 AC 109 ms
19,932 KB
testcase_73 AC 97 ms
15,772 KB
testcase_74 AC 62 ms
12,468 KB
testcase_75 AC 98 ms
16,532 KB
testcase_76 AC 72 ms
14,192 KB
testcase_77 AC 47 ms
8,880 KB
testcase_78 AC 55 ms
11,692 KB
testcase_79 AC 45 ms
9,580 KB
testcase_80 AC 102 ms
18,656 KB
testcase_81 AC 97 ms
16,036 KB
testcase_82 AC 74 ms
14,448 KB
testcase_83 AC 79 ms
14,688 KB
testcase_84 AC 62 ms
10,732 KB
testcase_85 AC 9 ms
4,376 KB
testcase_86 AC 62 ms
11,032 KB
testcase_87 AC 91 ms
16,916 KB
testcase_88 AC 36 ms
7,780 KB
testcase_89 AC 62 ms
8,416 KB
testcase_90 AC 31 ms
6,124 KB
testcase_91 AC 66 ms
8,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = (1LL << 62) - 1;
const long long MOD = 1000000007;

/**
 * @brief Dynamic-Li-Chao-Tree
 * @docs docs/dynamic-li-chao-tree.md
*/
template< typename T, T x_low, T x_high, T id >
struct DynamicLiChaoTree {

  struct Line {
    T a, b;

    Line(T a, T b) : a(a), b(b) {}

    inline T get(T x) const { return a * x + b; }
  };

  struct Node {
    Line x;
    Node *l, *r;

    Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {}
  };

  Node *root;

  DynamicLiChaoTree() : root{nullptr} {}

  Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(!t) return new Node(x);

    T t_l = t->x.get(l), t_r = t->x.get(r);

    if(t_l <= x_l && t_r <= x_r) {
      return t;
    } else if(t_l >= x_l && t_r >= x_r) {
      t->x = x;
      return t;
    } else {
      T m = (l + r) / 2;
      if(m == r) --m;
      T t_m = t->x.get(m), x_m = x.get(m);
      if(t_m > x_m) {
        swap(t->x, x);
        if(x_l >= t_l) t->l = add_line(t->l, x, l, m, t_l, t_m);
        else t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
      } else {
        if(t_l >= x_l) t->l = add_line(t->l, x, l, m, x_l, x_m);
        else t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
      }
      return t;
    }
  }

  void add_line(const T &a, const T &b) {
    Line x(a, b);
    root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l, const T &r, const T &x_l, const T &x_r) {
    if(r < a || b < l) return t;
    if(a <= l && r <= b) {
      Line y{x};
      return add_line(t, y, l, r, x_l, x_r);
    }
    if(t) {
      T t_l = t->x.get(l), t_r = t->x.get(r);
      if(t_l <= x_l && t_r <= x_r) return t;
    } else {
      t = new Node(Line(0, id));
    }
    T m = (l + r) / 2;
    if(m == r) --m;
    T x_m = x.get(m);
    t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
    t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
    return t;
  }

  void add_segment(const T &l, const T &r, const T &a, const T &b) {
    Line x(a, b);
    root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  T query(const Node *t, const T &l, const T &r, const T &x) const {
    if(!t) return id;
    if(l == r) return t->x.get(x);
    T m = (l + r) / 2;
    if(m == r) --m;
    if(x <= m) return min(t->x.get(x), query(t->l, l, m, x));
    else return min(t->x.get(x), query(t->r, m + 1, r, x));
  }

  T query(const T &x) const {
    return query(root, x_low, x_high, x);
  }
};

int main()
{  
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,a,b,w;
    cin>>n>>a>>b>>w;
    vec d(n);
    rep(i,n)cin>>d[i];
    vec dp(n+1,INF);
    dp[0]=0;
    DynamicLiChaoTree<ll,0,300005,INF> cht;
    rep(i,n){
      dp[i]=b*(i)*(i+1)/2-a*(i)+d[i];
      if(i)chmin(dp[i],cht.query(i)+(i)*(i+1)*b/2-a*(i)+d[i]);
      cht.add_line(-(i+1)*b,i*(i+1)*b/2+a*(i+1)+dp[i]);
    }
    ll ans=b*(n)*(n+1)/2-a*n;
    chmin(ans,cht.query(n)+n*(n+1)*b/2-a*n);
    cout<<ans+w<<endl;
    return 0;
}
0