結果
問題 |
No.409 ダイエット
|
ユーザー |
![]() |
提出日時 | 2016-08-06 00:27:39 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 1,229 bytes |
コンパイル時間 | 1,650 ms |
コンパイル使用メモリ | 162,660 KB |
実行使用メモリ | 12,792 KB |
最終ジャッジ日時 | 2024-06-26 09:43:53 |
合計ジャッジ時間 | 5,383 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 92 |
ソースコード
#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; long long another = f[i - 1] + d[i]; Line d1 = {-1LL * i * b, another + 1LL * i * a + i * (i - 1LL) / 2 * b, 1e50}; 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); f[i] = min(f[i], another); } cout << f[n] + w << '\n'; return 0; }