結果

問題 No.2458 Line Up Charged Balls
ユーザー sotanishysotanishy
提出日時 2023-09-01 23:25:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 2,201 ms
コンパイル使用メモリ 207,324 KB
実行使用メモリ 7,872 KB
最終ジャッジ日時 2023-09-01 23:25:53
合計ジャッジ時間 4,897 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 52 ms
7,808 KB
testcase_06 AC 52 ms
7,764 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 67 ms
7,328 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 39 ms
5,492 KB
testcase_14 AC 38 ms
5,580 KB
testcase_15 AC 20 ms
5,120 KB
testcase_16 AC 60 ms
7,616 KB
testcase_17 AC 64 ms
7,712 KB
testcase_18 AC 62 ms
7,632 KB
testcase_19 AC 72 ms
7,772 KB
testcase_20 AC 69 ms
7,692 KB
testcase_21 AC 59 ms
7,764 KB
testcase_22 AC 64 ms
7,704 KB
testcase_23 AC 78 ms
7,808 KB
testcase_24 AC 66 ms
7,872 KB
testcase_25 AC 77 ms
7,700 KB
testcase_26 AC 70 ms
7,796 KB
testcase_27 AC 77 ms
7,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

template <typename T>
class ConvexHullTrick {
   public:
    void add(T a, T b) {
        a = -a, b = -b;
        auto m = lines.insert({a, b, 0});
        auto l = m, r = m;
        ++r;
        while (update(m, r)) {
            r = lines.erase(r);
        }
        if (l != lines.begin() && update(--l, m)) {
            m = lines.erase(m);
            update(l, m);
        }
        m = l;
        while (l != lines.begin() && (--l)->p >= m->p) {
            update(l, lines.erase(m));
            m = l;
        }
    }

    T get(T x) const {
        assert(!lines.empty());
        auto it = *lines.lower_bound(x);
        return -(it.a * x + it.b);
    }

   private:
    struct Line {
        mutable T a, b;    // ax + b
        mutable double p;  // intersection point with the next line
        bool operator<(const Line& o) const { return a < o.a; }
        bool operator<(T x) const { return p < x; }
    };

    using iterator = typename std::multiset<Line, std::less<>>::iterator;
    static constexpr double INF = std::numeric_limits<double>::max() / 2;

    std::multiset<Line, std::less<>> lines;

    bool update(iterator x, iterator y) const {
        if (y == lines.end()) {
            x->p = INF;
            return false;
        }
        if (x->a == y->a) {
            x->p = (x->b > y->b ? INF : -INF);
        } else {
            x->p = 1.0 * (y->b - x->b) / (x->a - y->a);
        }
        return x->p >= y->p;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    vector<ll> Q(N);
    for (auto& x : Q) cin >> x;
    vector<ll> dp(N);
    ConvexHullTrick<ll> cht;
    cht.add(-Q[0], 0);
    ll ans = 0;
    rep(i, 1, N) {
        dp[i] = -cht.get(Q[i]);
        cht.add(-Q[i], -max(0LL, dp[i]));
        chmax(ans, dp[i]);
    }
    cout << ans << endl;
}
0