結果
問題 | No.409 ダイエット |
ユーザー |
|
提出日時 | 2020-12-25 17:02:29 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 104 ms / 2,000 ms |
コード長 | 2,942 bytes |
コンパイル時間 | 1,776 ms |
コンパイル使用メモリ | 175,352 KB |
実行使用メモリ | 13,032 KB |
最終ジャッジ日時 | 2024-09-22 10:11:03 |
合計ジャッジ時間 | 6,910 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 92 |
ソースコード
#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; }