結果

問題 No.409 ダイエット
ユーザー dnkdnk
提出日時 2016-08-06 00:26:55
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 1,510 bytes
コンパイル時間 1,380 ms
コンパイル使用メモリ 163,548 KB
実行使用メモリ 12,660 KB
最終ジャッジ日時 2024-06-26 09:43:38
合計ジャッジ時間 4,998 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 92
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int N = (int) 3e5 + 1;
int a, b, w, d[N], n;
long long f[N];

struct Line {
    long long a, b; // ax + b
    double lim;

    double cut(const Line &other) {
        return -(b - other.b) / (double) (a - other.a);
    }

    long long x(int z) {
        return a * z + b;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin >> n >> a >> b >> w;
    for (int i = 1; i <= n; ++i) cin >> d[i];
    if (b == 0) {
        cout << w - 1LL * a * n << endl;
        return 0;
    }
    vector<Line> lines;
    lines.push_back({0, 0, 1e50});
    int ptr = 0;
    for (int i = 1; i <= n; ++i) {
        ptr = min(ptr, (int) lines.size() - 1);
        while (i > lines[ptr].lim) ++ptr;
        f[i] = lines[ptr].x(i) - 1LL * i * a + i * (i + 1LL) / 2 * b; //cerr << "*" << f[i] << ' ' << ptr << endl;
        long long another = f[i - 1] + d[i]; //cerr << "ANOTHER " << another << endl;
        Line d1 = {-1LL * i * b, another + 1LL * i * a + i * (i - 1LL) / 2 * b, 1e50};
        //cerr << d1.a << ' ' << d1.b << " LINE" << endl;
        while (lines.size() > 1 && d1.cut(lines[lines.size() - 1]) <= d1.cut(lines[lines.size() - 2])) lines.pop_back();
        lines.back().lim = d1.cut(lines.back());
        lines.push_back(d1);
        //for (auto x : lines) cerr << x.lim << ' '; cerr << endl;
        f[i] = min(f[i], another);
    }
    //for (int i = 1; i <= n; ++i) cout << f[i] << ' '; cout << '\n';
    cout << f[n] + w << '\n';
    return 0;
}
0