結果

問題 No.409 ダイエット
ユーザー 🍮かんプリン
提出日時 2020-10-15 03:02:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,557 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 174,416 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-07-20 19:36:43
合計ジャッジ時間 7,792 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.15 03:02:41
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;





template<typename T>
struct ConvexHullTrick {
private:
    using P = pair<T,T>;
    deque<P> deq;
    
    inline bool check(P l, P m, P r) {
        return (m.first-l.first)*(r.second-m.second) >= (m.second-l.second)*(r.first-m.first);
    }
public:
    ConvexHullTrick() {}
    
    void add_query(T a, T b) {
        if (deq.size() <= 1) {
            deq.push_back(make_pair(a,b));
            return;
        }
        P m = deq.back(); deq.pop_back();
        while(!deq.empty() && check(deq.back(),m,{a,b})) {
            m = deq.back(); deq.pop_back();
        }
        deq.push_back(m); deq.push_back({a,b});
    }
    
    T get_query(T x) {
        P m = deq.front(); deq.pop_front();
        while(!deq.empty() && m.first * x + m.second >= deq.front().first * x + deq.front().second) { 
            m = deq.front(); deq.pop_front();
        }
        deq.push_front(m);
        return m.first * x + m.second;
    }
};
int main() {
    ConvexHullTrick<ll> cht;
    ll n,a,b,w;cin >> n >> a >> b >> w;
    vector<ll> dp(n+1);
    dp[0] = 0;
    cht.add_query(0,a);
    constexpr long long LLINF = 1e18 + 1;
    for (ll i = 1; i <= n; i++) {
        ll d;cin >> d;
        dp[i] = cht.get_query(i)-i*a+i*(i-1)/2*b+d;
        cht.add_query(-b*i,dp[i]+(i+1)*a+i*(i+1)/2*b);
    }
    ll ans = cht.get_query(n+1)-(n+1)*a+(n+1)*n/2*b;
    cout << min(dp[n],ans) + w << endl;
    return 0;
}
0