結果

問題 No.409 ダイエット
ユーザー veqccveqcc
提出日時 2019-09-15 16:54:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 2,497 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 78,580 KB
実行使用メモリ 13,304 KB
最終ジャッジ日時 2023-09-21 04:09:29
合計ジャッジ時間 6,160 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,380 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 2 ms
4,376 KB
testcase_47 AC 3 ms
4,376 KB
testcase_48 AC 3 ms
4,380 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 32 ms
7,252 KB
testcase_56 AC 55 ms
7,576 KB
testcase_57 AC 64 ms
12,492 KB
testcase_58 AC 28 ms
7,044 KB
testcase_59 AC 40 ms
7,816 KB
testcase_60 AC 25 ms
6,820 KB
testcase_61 AC 56 ms
12,304 KB
testcase_62 AC 58 ms
11,944 KB
testcase_63 AC 55 ms
12,168 KB
testcase_64 AC 31 ms
7,152 KB
testcase_65 AC 57 ms
9,160 KB
testcase_66 AC 62 ms
12,840 KB
testcase_67 AC 47 ms
11,628 KB
testcase_68 AC 39 ms
8,084 KB
testcase_69 AC 55 ms
13,224 KB
testcase_70 AC 80 ms
11,452 KB
testcase_71 AC 46 ms
7,272 KB
testcase_72 AC 97 ms
13,304 KB
testcase_73 AC 91 ms
11,760 KB
testcase_74 AC 58 ms
10,800 KB
testcase_75 AC 89 ms
11,804 KB
testcase_76 AC 66 ms
10,688 KB
testcase_77 AC 44 ms
6,028 KB
testcase_78 AC 50 ms
9,396 KB
testcase_79 AC 40 ms
6,924 KB
testcase_80 AC 91 ms
11,740 KB
testcase_81 AC 88 ms
12,512 KB
testcase_82 AC 66 ms
10,428 KB
testcase_83 AC 72 ms
11,516 KB
testcase_84 AC 58 ms
7,768 KB
testcase_85 AC 8 ms
4,376 KB
testcase_86 AC 57 ms
7,644 KB
testcase_87 AC 79 ms
11,852 KB
testcase_88 AC 33 ms
5,516 KB
testcase_89 AC 65 ms
6,672 KB
testcase_90 AC 31 ms
5,040 KB
testcase_91 AC 69 ms
6,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
using namespace std;
typedef long long ll;

template<typename T>
class ConvexHullTrick {
private:
    typedef pair <T, T> P;
    vector <P> lines;
    bool isMonotonic;
    function<bool(T l, T r)> comp;
public:
    ConvexHullTrick(bool M = false, function<bool(T l, T r)> c = [](T l, T r) { return l >= r; })
            : isMonotonic(M), comp(c) {}
    bool check(P l1, P l2, P l3) {
        if (l1 < l3) swap(l1, l3);
        return (l3.second - l2.second) * (l2.first - l1.first) >= (l2.second - l1.second) * (l3.first - l2.first);
    }
    void add(T a, T b) {
        P line = P(a, b);
        while (lines.size() >= 2 && check(*(lines.end() - 2), lines.back(), line)) lines.pop_back();
        lines.push_back(line);
    }
    T f(int k, T x) { return lines[k].first * x + lines[k].second; }
    T f(P line, T x) { return line.first * x + line.second; }
    T get(T x) {
        if (isMonotonic) {
            static int head = 0;
            while (lines.size() - head >= 2 && comp(f(head, x), f(head + 1, x))) head++;
            return f(head, x);
        } else {
            int lb = -1, ub = lines.size() - 1;
            while (ub - lb > 1) {
                int md = (ub + lb) / 2;
                if (comp(f(md, x), f(md + 1, x))) lb = md;
                else ub = md;
            }
            return f(ub, x);
        }
    }
};


// verified
//  https://atcoder.jp/contests/colopl2018-final/tasks/colopl2018_final_c
void AtCoder_2018_1_20_C() {
    ll N, a;
    cin >> N;
    ConvexHullTrick<ll> CHT(true);
    for (ll i = 0; i < N; i++) {
        cin >> a;
        CHT.add(-2 * i, a + i * i);
    }
    for (ll i = 0; i < N; i++) cout << CHT.get(i) + i * i << '\n';
}


// verified
//  https://yukicoder.me/problems/no/409
void yuki409() {
    ll n, a, b, w;
    cin >> n >> a >> b >> w;
    vector <ll> d(n);
    for (int i = 0; i < n; i++) cin >> d[i];
    ConvexHullTrick<ll> CHT(true);
    vector <ll> dp(n + 1); // i日目に食べた時の体重の最大の変化量
    CHT.add(0, 0);
    for (ll i = 1; i <= n; i++) {
        dp[i] = CHT.get(i - 1) - a * (i - 1) + b * i * (i - 1) / 2 + d[i - 1];
        CHT.add(-i * b, dp[i] + a * i + b * i * (i - 1) / 2);
    }
    ll ans = dp[n];
    for (ll i = 0; i < n; i++) {
        ans = min(ans, dp[i] - a * (n - i) + b * (n - i) * (n - i + 1) / 2);
    }
    cout << ans + w << '\n';
}


int main() {
    // AtCoder_2018_1_20_C();
    yuki409();
    return 0;
}
0