結果
| 問題 |
No.409 ダイエット
|
| コンテスト | |
| ユーザー |
cutmdo
|
| 提出日時 | 2023-03-12 04:32:34 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 165 ms / 2,000 ms |
| コード長 | 3,686 bytes |
| コンパイル時間 | 965 ms |
| コンパイル使用メモリ | 93,452 KB |
| 最終ジャッジ日時 | 2025-02-11 10:34:00 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 92 |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/no/409"
#include <iostream>
#include <memory>
#include <vector>
#include <numeric>
// X_MAX: ax+bであるxとして取りうる最大値
template<long long X_MAX, class T = long long>
class DynamicLiChaoTree {
constexpr static T INF = 2e18;
class Line {
T a, b;
public:
Line(const T& a, const T& b) :a(a), b(b) {}
Line() :Line(0, INF) {}
Line(const Line& other) noexcept :Line(other.a, other.b) {}
Line(Line&& other) noexcept :Line(other) {}
Line& operator=(Line&& other) noexcept {
if(this != &other) { a = other.a; b = other.b; }
return *this;
}
auto operator<(const Line& line)const { return a < line.a; }
auto operator>(const Line& line)const { return line.operator<(*this); }
auto f(const T& x)const { return a * x + b; }
auto debug()const { std::cerr << "(" << a << " " << b << ")" << std::endl; }
};
struct Node {
Line line;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
Node(const Line& line) :line(line) {}
auto f(const T& x)const { return line.f(x); }
};
std::unique_ptr<Node> m_root;
auto add(std::unique_ptr<Node>& node, Line&& line, long long l, long long r) {
if(!node) { node = std::make_unique<Node>(line); return; }
auto m = (l + r) / 2;
if(line.f(m) < node->f(m)) { std::swap(node->line, line); }
if(l + 1 == r) { return; }
if(line > node->line) {
add(node->left, std::move(line), l, m);
} else if(line < node->line) {
add(node->right, std::move(line), m, r);
}
}
auto query(const std::unique_ptr<Node>& node, const T& x, long long l, long long r) const {
if(!node) { return Line().f(x); }
auto m = (l + r) / 2;
return std::min(
node->f(x),
(x < m) ? query(node->left, x, l, m) : query(node->right, x, m, r)
);
}
public:
DynamicLiChaoTree() {}
auto add(const T& a, const T& b) { add(m_root, Line(a, b), -X_MAX, X_MAX); }
auto add(const std::pair<T, T>& line) { add(line.first, line.second); }
auto query(const T& x) const { return query(m_root, x, -X_MAX, X_MAX); }
auto debug(const std::unique_ptr<Node>& node, int size)const {
if(size == 0) { std::cerr << "-- li chao tree --" << std::endl; }
if(!node) { return; }
for(int i = 0; i < size; ++i) { std::cerr << "- "; }
node->line.debug();
if(node->left) { std::cout << "L"; debug(node->left, size + 1); }
if(node->right) { std::cout << "R"; debug(node->right, size + 1); }
}
auto debug()const { debug(m_root, 0); }
};
using ll = long long;
using std::cout;
using std::cin;
constexpr char endl = '\n';
signed main() {
ll n, a, b, w;
cin >> n >> a >> b >> w;
std::vector<ll> d; d.reserve(n);
for(int i = 0; i < n; ++i) {
ll x; cin >> x;
d.emplace_back(x);
}
constexpr ll mx = 1e18;
std::vector<ll> dp(n + 1, mx);
DynamicLiChaoTree<static_cast<ll>(1e6)> lct;
auto update = [&](ll i, ll x) {
dp[i] = x;
auto pa = -b * i;
auto pb = a * i + i * (i + 1) / 2 * b + dp[i];
lct.add(pa, pb);
};
update(0, w);
for(ll i = 1; i < n + 1; ++i) {
auto ad = d[i - 1] - a * i + a + i * (i - 1) / 2 * b;
auto min = lct.query(i);
update(i, ad + min);
}
ll ans = dp[n];
for(ll i = 0; i < n; ++i) {
ll k = n - i;
ans = std::min(ans, dp[i] + -a * k + k * (k + 1) / 2 * b);
}
cout << ans << endl;
}
cutmdo