結果
| 問題 | No.409 ダイエット |
| コンテスト | |
| ユーザー |
はまやんはまやん
|
| 提出日時 | 2017-03-24 16:15:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,993 bytes |
| 記録 | |
| コンパイル時間 | 1,657 ms |
| コンパイル使用メモリ | 173,136 KB |
| 実行使用メモリ | 7,248 KB |
| 最終ジャッジ日時 | 2024-07-06 01:56:52 |
| 合計ジャッジ時間 | 5,163 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 84 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long double ld;
template<typename T> struct CHT {
struct line {
T slope, xterm;
inline T get(const int x) const { return slope * x + xterm; }
};
deque<line> lines;
inline ld area(const line& a, const line& b, const line& c) {
ld ax = (b.slope - a.slope);
ld bx = (c.xterm - a.xterm);
ld ay = (c.slope - a.slope);
ld by = (b.xterm - a.xterm);
return ax * bx - ay * by;
}
inline bool cw(const line& a, const line& b, const line& c) {
return area(a, b, c) < 0;
}
void add(T a, T b) {
line l;
l.slope = a, l.xterm = b;
while (lines.size() > 1 && cw(lines[lines.size() - 2], lines[lines.size() - 1], l))
lines.pop_back();
if (lines.size() == 1) {
if (lines.front().xterm > l.xterm)
lines.push_back(l);
}
else lines.push_back(l);
}
T get(int x) {
if (lines.empty())
return 0;
while (lines.size() > 1 && lines[0].get(x) > lines[1].get(x))
lines.pop_front();
return lines[0].get(x);
}
};
//-----------------------------------------------------------------------------------
typedef long long ll;
#define INF 1LL<<60
int N, A, B, W;
int D[301010];
//-----------------------------------------------------------------------------------
ll dp[301010];
int main() {
cin >> N >> A >> B >> W;
rep(i, 0, N) scanf("%d", &D[i]);
/* Naive
dp[0] = W;
rep(i, 1, N + 1) {
dp[i] = INF;
rep(j, 0, i) {
ll w = dp[j];
w -= 1LL * A * (i - j - 1);
w += 1LL * (i - j - 1) * (i - j) / 2 * B;
dp[i] = min(dp[i], w);
}
dp[i] += D[i - 1];
}
*/
// Optimized
CHT<ll> cht;
dp[0] = W;
cht.add(0, W);
rep(i, 1, N + 1) {
dp[i] = cht.get(i) - 1LL * (i - 1) * A + (1LL * (i - 1) * i) / 2 * B + D[i - 1];
cht.add(-1LL * B * i, dp[i] + 1LL * A * i + (1LL * i * i + i) / 2 * B);
}
ll ans = INF;
rep(i, 0, N + 1) {
ll w = dp[i];
int n = N - i;
w -= 1LL * A * n;
w += 1LL * n * (n + 1) / 2 * B;
ans = min(ans, w);
}
cout << ans << endl;
}
はまやんはまやん