結果

問題 No.409 ダイエット
ユーザー paruki
提出日時 2018-02-20 18:50:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 2,286 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 177,876 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-26 10:29:24
合計ジャッジ時間 5,648 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
#define vv(a,b,c,d) vector<vector<a> >(b,vector<a>(c,d))
#define vvv(a,b,c,d,e) vector<vector<vector<a> > >(b,vv(a,c,d,e))
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class Val, class Compare>
struct ConvexHullTrick {
    deque<pair<Val, Val> > lines;
    Compare comp;
    ConvexHullTrick() :comp(Compare()) {
    }
    void addLine(Val slope, Val constant) {
        pair<Val, Val> line(slope, constant);
        int n;
        while ((n = (int)lines.size())>=2) {
            Val a1, a2, a3, b1, b2, b3;
            tie(a1, b1) = lines[n - 2];
            tie(a2, b2) = lines[n - 1];
            tie(a3, b3) = line;
            if ((a1 - a2)*(b3 - b1) >(a1 - a3)*(b2 - b1))break;
            else lines.pop_back();
        }
        lines.push_back(line);
    }
    Val query(Val x) {
        while ((int)lines.size() >= 2 && comp(f(x, lines[1]), f(x, lines[0]))) {
            lines.pop_front();
        }
        return f(x, lines[0]);
    }
    Val f(Val x, pair<Val, Val> line) {
        return x * line.first + line.second;
    }
};

/*
解説のとおりの実装
ライブラリテスト
*/
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int N, A, B, W;
    cin >> N >> A >> B >> W;
    vi D(N + 1);
    for (int i = 1; i <= N; ++i)cin >> D[i];

    ConvexHullTrick<ll, less<ll> > cht;
    cht.addLine(0, W);
    vll F(N + 1);
    F[0] = W;

    for (ll k = 1; k <= N; ++k) {
        F[k] = D[k] - (k - 1)*A + (k*k - k)*B / 2;
        F[k] += cht.query(k);
        cht.addLine(-B*k, F[k] + k*A + (k*k + k)*B / 2);
    }

    ll ans = LLONG_MAX;
    for (ll k = 0; k <= N; ++k) {
        smin(ans, F[k] - (N - k)*A + (N - k + 1)*(N - k)*B / 2);
    }
    cout << ans << endl;
}
0