結果

問題 No.409 ダイエット
ユーザー Div9851Div9851
提出日時 2020-02-27 00:39:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 3,224 bytes
コンパイル時間 2,120 ms
コンパイル使用メモリ 171,864 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-04-21 16:42:23
合計ジャッジ時間 5,817 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 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 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 3 ms
5,376 KB
testcase_41 AC 3 ms
5,376 KB
testcase_42 AC 3 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 2 ms
5,376 KB
testcase_50 AC 3 ms
5,376 KB
testcase_51 AC 3 ms
5,376 KB
testcase_52 AC 3 ms
5,376 KB
testcase_53 AC 2 ms
5,376 KB
testcase_54 AC 2 ms
5,376 KB
testcase_55 AC 30 ms
5,376 KB
testcase_56 AC 52 ms
7,680 KB
testcase_57 AC 58 ms
7,680 KB
testcase_58 AC 26 ms
5,376 KB
testcase_59 AC 37 ms
6,144 KB
testcase_60 AC 22 ms
5,376 KB
testcase_61 AC 52 ms
7,168 KB
testcase_62 AC 54 ms
7,424 KB
testcase_63 AC 52 ms
7,168 KB
testcase_64 AC 28 ms
5,376 KB
testcase_65 AC 56 ms
7,296 KB
testcase_66 AC 58 ms
7,680 KB
testcase_67 AC 44 ms
6,528 KB
testcase_68 AC 40 ms
6,016 KB
testcase_69 AC 50 ms
7,168 KB
testcase_70 AC 73 ms
6,912 KB
testcase_71 AC 43 ms
5,376 KB
testcase_72 AC 96 ms
7,680 KB
testcase_73 AC 88 ms
7,424 KB
testcase_74 AC 55 ms
5,888 KB
testcase_75 AC 90 ms
7,168 KB
testcase_76 AC 62 ms
6,272 KB
testcase_77 AC 42 ms
5,376 KB
testcase_78 AC 47 ms
5,376 KB
testcase_79 AC 37 ms
5,376 KB
testcase_80 AC 88 ms
7,552 KB
testcase_81 AC 86 ms
7,424 KB
testcase_82 AC 62 ms
6,400 KB
testcase_83 AC 66 ms
6,528 KB
testcase_84 AC 58 ms
6,016 KB
testcase_85 AC 8 ms
5,376 KB
testcase_86 AC 56 ms
5,632 KB
testcase_87 AC 78 ms
6,912 KB
testcase_88 AC 33 ms
5,376 KB
testcase_89 AC 67 ms
6,400 KB
testcase_90 AC 30 ms
5,376 KB
testcase_91 AC 70 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;

template <typename T, bool isInc = true, bool isMin = true>
struct ConvexHullTrickAddMonotone {
    using P = pair<T, T>;
    deque<P> H;

    bool empty() const { return H.empty(); }

    void clear() { H.clear(); }

    inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

    using D = long double;

    inline bool check(const P &a, const P &b, const P &c) {
        if (b.second == a.second || c.second == b.second)
            return sgn(b.first - a.first) * sgn(c.second - b.second) >=
                   sgn(c.first - b.first) * sgn(b.second - a.second);
        return D(b.first - a.first) * sgn(c.second - b.second) /
                   D(abs(b.second - a.second)) >=
               D(c.first - b.first) * sgn(b.second - a.second) /
                   D(abs(c.second - b.second));
    }

    void add(T a, T b) {
        if (!isMin) a *= -1, b *= -1;
        P line(a, b);
        if (empty()) {
            H.emplace_front(line);
            return;
        }
        if (isInc ^ (!isMin)) {  // 傾きが単調増加
            if (H.front().first == a) {
                if (H.front().second <= b) return;
                H.pop_front();
            }
            while (H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
            H.emplace_front(line);
        } else {  // 傾きが単調減少
            if (H.back().first == a) {
                if (H.back().second <= b) return;
                H.pop_back();
            }
            while (H.size() >= 2 && check(H[H.size() - 2], H.back(), line))
                H.pop_back();
            H.emplace_back(line);
        }
    }

    inline T get_y(const P &a, const T &x) { return a.first * x + a.second; }

    T query(T x) {
        int l = -1, r = H.size() - 1;
        while ((r - l) > 1) {
            int m = (l + r) / 2;
            if (get_y(H[m], x) >= get_y(H[m + 1], x))
                l = m;
            else
                r = m;
        }
        if (isMin) return get_y(H[r], x);
        return -get_y(H[r], x);
    }

    T query_monotone_inc(T x) {
        while (H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x))
            H.pop_front();
        if (isMin)
            return get_y(H.front(), x);
        else
            return -get_y(H.front(), x);
    }

    T query_monotone_dec(T x) {
        while (H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x))
            H.pop_back();
        if (isMin)
            return get_y(H.back(), x);
        else
            return -get_y(H.back(), x);
    }
};

int main() {
    int N;
    ll A, B, W;
    cin >> N >> A >> B >> W;
    vector<ll> D(N + 1);
    for (int i = 1; i <= N; i++) cin >> D[i];
    vector<ll> dp(N + 1);
    ConvexHullTrickAddMonotone<ll, false, true> cht;
    cht.add(0, 0);
    for (ll i = 1; i <= N; i++) {
        dp[i] = cht.query_monotone_inc(i) - A * (i - 1) + (i * i - i) / 2 * B +
                D[i];
        cht.add(-i * B, dp[i] + A * i + (i * i + i) / 2 * B);
    }
    ll ans = 1LL << 60;
    for (ll i = 0; i <= N; i++) {
        ans = min(ans, dp[i] - A * (N - i) + (N - i) * (N - i + 1) / 2 * B + W);
    }
    cout << ans << endl;
}
0