結果

問題 No.409 ダイエット
ユーザー pyraninepyranine
提出日時 2020-12-25 17:02:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 2,942 bytes
コンパイル時間 1,992 ms
コンパイル使用メモリ 175,108 KB
実行使用メモリ 12,120 KB
最終ジャッジ日時 2023-10-23 14:53:58
合計ジャッジ時間 7,975 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 3 ms
4,348 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 3 ms
4,348 KB
testcase_46 AC 3 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 3 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 3 ms
4,348 KB
testcase_53 AC 3 ms
4,348 KB
testcase_54 AC 3 ms
4,348 KB
testcase_55 AC 36 ms
7,424 KB
testcase_56 AC 61 ms
7,804 KB
testcase_57 AC 68 ms
11,972 KB
testcase_58 AC 30 ms
6,940 KB
testcase_59 AC 42 ms
7,812 KB
testcase_60 AC 27 ms
6,844 KB
testcase_61 AC 61 ms
11,404 KB
testcase_62 AC 65 ms
11,596 KB
testcase_63 AC 60 ms
11,436 KB
testcase_64 AC 33 ms
7,364 KB
testcase_65 AC 61 ms
9,268 KB
testcase_66 AC 67 ms
11,964 KB
testcase_67 AC 55 ms
10,528 KB
testcase_68 AC 42 ms
7,780 KB
testcase_69 AC 58 ms
11,448 KB
testcase_70 AC 86 ms
10,960 KB
testcase_71 AC 49 ms
7,260 KB
testcase_72 AC 103 ms
11,884 KB
testcase_73 AC 96 ms
12,032 KB
testcase_74 AC 62 ms
11,652 KB
testcase_75 AC 96 ms
11,892 KB
testcase_76 AC 74 ms
12,120 KB
testcase_77 AC 46 ms
6,412 KB
testcase_78 AC 55 ms
11,472 KB
testcase_79 AC 43 ms
7,176 KB
testcase_80 AC 99 ms
11,460 KB
testcase_81 AC 95 ms
11,940 KB
testcase_82 AC 71 ms
10,364 KB
testcase_83 AC 76 ms
10,544 KB
testcase_84 AC 64 ms
7,824 KB
testcase_85 AC 8 ms
4,348 KB
testcase_86 AC 61 ms
7,708 KB
testcase_87 AC 85 ms
10,932 KB
testcase_88 AC 35 ms
5,784 KB
testcase_89 AC 70 ms
7,052 KB
testcase_90 AC 33 ms
5,196 KB
testcase_91 AC 74 ms
7,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int8 = int8_t; using Int16 = int16_t; using Int32 = int32_t; using Int64 = int64_t; using Int128 = __int128_t;
using Word8 = uint8_t; using Word16 = uint16_t; using Word32 = uint32_t; using Word64 = uint64_t; using Word128 = __uint128_t;
using Int = int_fast64_t;
using Word = uint_fast64_t;
using VI = vector<Int>; using VW = vector<Word>; using VVI = vector<VI>; using VVW = vector<VW>;
using F32 = float; using F64 = double; using F80 = long double;
using PII = pair<Int,Int>; using PWW = pair<Word,Word>; using VPII = vector<PII>; using VPWW = vector<PWW>;
using VS = vector<string>; using VVS = vector<VS>; using VB = vector<bool>; using VVB = vector<VB>;

#define rep(i,n) for(Int i=0,i##_len=(n); i<i##_len ; i++)
#define reps(i,n) for(Int i=0,i##_len=(n); i<=i##_len; i++)
#define Range(i,a,b) for(Int i=(a); i<=(b); i++)
#define SZ(obj) ((Int)obj.size())
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define UNIQUE(obj) (obj).erase(unique((obj).begin(), (obj).end()), (obj).end())

template<class T>
class ConvexHullTrick {
private:
    vector<pair<T,T>> lines;
    bool is_monotonic;
public:
    ConvexHullTrick(bool flag = false) : is_monotonic(flag) {
        lines.emplace_back(0, 0);
    }

    bool check(pair<T,T> l1, pair<T,T> l2, pair<T,T> l3) {
        return (l2.first - l1.first) * (l3.second - l2.second) >= (l2.second - l1.second) * (l3.first - l2.first);
    }

    void add(pair<T,T> l) {
        pair<T,T> line(l);
        while (lines.size() >= 2 && check(*(lines.end() - 2), lines.back(), line)) lines.pop_back();
        lines.emplace_back(line);
    }

    void add(T a, T b) {
        pair<T,T> line(a, b);
        add(line);
    }

    T f(int i, T x) {
        return lines[i].first * x + lines[i].second;
    }

    T get(T x, function<bool(T l, T r)> comp = [](T l, T r) { return l >= r; }) {
        if (is_monotonic) {
            static int head = 0;
            while (lines.size() - head >= 2 && comp(f(head, x), f(head + 1, x))) ++head;
            return f(head, x);
        }
        else {
            int low = 0, high = lines.size() - 1;
            while (high - low > 1) {
                int mid = (high - low) / 2;
                (comp(f(mid, x), f(mid + 1, x)) ? low : high) = mid;
            }
            return f(high, x);
        }
	}
};

/**
 * yukicoder No409. ダイエット
 **/
int main () {
    Int N, A, B, W; cin >> N >> A >> B >> W; VI d(N); rep(i,N) cin >> d[i];
    
    ConvexHullTrick<Int> cht(true);
    Int dp[300005]; dp[0] = 0;
    Range(i,1,N) {
        dp[i] = cht.get(i - 1) - (i - 1) * A + ((Int)(i - 1) * i) / 2 * B + d[i - 1];
        cht.add(- i * B, dp[i] + i * A + (Int)(i - 1) * i / 2 * B);
    }
    Int ans = 1LL << 60;
    rep(i,(N + 1)) {
        ans = min(ans, dp[i] + (- (N - i) * A + (N - i) * (N - i + 1) / 2 * B));
    }
    cout << ans + W << endl;
}
0