結果

問題 No.409 ダイエット
ユーザー cutmdocutmdo
提出日時 2023-03-12 04:32:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 3,686 bytes
コンパイル時間 1,216 ms
コンパイル使用メモリ 98,848 KB
実行使用メモリ 21,536 KB
最終ジャッジ日時 2023-10-18 10:27:52
合計ジャッジ時間 7,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 3 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 4 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 4 ms
4,348 KB
testcase_43 AC 4 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 4 ms
4,348 KB
testcase_46 AC 4 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 4 ms
4,348 KB
testcase_50 AC 3 ms
4,348 KB
testcase_51 AC 4 ms
4,348 KB
testcase_52 AC 4 ms
4,348 KB
testcase_53 AC 4 ms
4,348 KB
testcase_54 AC 4 ms
4,348 KB
testcase_55 AC 61 ms
12,032 KB
testcase_56 AC 55 ms
7,808 KB
testcase_57 AC 121 ms
21,536 KB
testcase_58 AC 52 ms
10,976 KB
testcase_59 AC 76 ms
14,144 KB
testcase_60 AC 46 ms
10,184 KB
testcase_61 AC 107 ms
19,160 KB
testcase_62 AC 113 ms
20,216 KB
testcase_63 AC 106 ms
19,160 KB
testcase_64 AC 58 ms
11,504 KB
testcase_65 AC 113 ms
18,896 KB
testcase_66 AC 119 ms
21,008 KB
testcase_67 AC 90 ms
16,784 KB
testcase_68 AC 74 ms
14,144 KB
testcase_69 AC 105 ms
18,632 KB
testcase_70 AC 129 ms
17,840 KB
testcase_71 AC 72 ms
11,504 KB
testcase_72 AC 156 ms
21,272 KB
testcase_73 AC 145 ms
19,424 KB
testcase_74 AC 91 ms
13,616 KB
testcase_75 AC 145 ms
19,688 KB
testcase_76 AC 105 ms
15,200 KB
testcase_77 AC 71 ms
10,976 KB
testcase_78 AC 80 ms
12,296 KB
testcase_79 AC 64 ms
10,448 KB
testcase_80 AC 148 ms
20,216 KB
testcase_81 AC 144 ms
19,424 KB
testcase_82 AC 106 ms
15,464 KB
testcase_83 AC 115 ms
16,256 KB
testcase_84 AC 95 ms
13,616 KB
testcase_85 AC 11 ms
4,496 KB
testcase_86 AC 92 ms
13,352 KB
testcase_87 AC 129 ms
17,840 KB
testcase_88 AC 53 ms
9,128 KB
testcase_89 AC 104 ms
11,504 KB
testcase_90 AC 50 ms
7,808 KB
testcase_91 AC 110 ms
11,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}

0