結果

問題 No.2458 Line Up Charged Balls
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-10 11:31:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 2,586 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 201,928 KB
実行使用メモリ 8,028 KB
最終ジャッジ日時 2023-08-28 19:35:20
合計ジャッジ時間 6,374 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 100 ms
7,832 KB
testcase_06 AC 101 ms
7,768 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 84 ms
7,340 KB
testcase_10 AC 24 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 47 ms
5,420 KB
testcase_14 AC 48 ms
5,444 KB
testcase_15 AC 23 ms
5,100 KB
testcase_16 AC 78 ms
7,712 KB
testcase_17 AC 87 ms
8,028 KB
testcase_18 AC 83 ms
7,656 KB
testcase_19 AC 88 ms
7,772 KB
testcase_20 AC 78 ms
7,748 KB
testcase_21 AC 65 ms
7,876 KB
testcase_22 AC 67 ms
7,872 KB
testcase_23 AC 94 ms
7,772 KB
testcase_24 AC 84 ms
7,724 KB
testcase_25 AC 96 ms
7,728 KB
testcase_26 AC 79 ms
7,704 KB
testcase_27 AC 93 ms
7,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<typename T> class CHT {
private:
    struct node {
        node *left, *right;
        static const T inf = numeric_limits<T>::max();
        T a, b;
        node() : node(0, inf){}
        node(const T _a, const T _b)
            : left(nullptr), right(nullptr), a(_a), b(_b){}
        T f(const T x) const { return a * x + b; }
    };
    static void swap(node *x, node *y){
        std::swap(x->a, y->a), std::swap(x->b, y->b);
    }
    void _add_line(node *cur, node *nw, T l, T r){
        while(true){
            if(nw->f(l) < cur->f(l)) swap(cur, nw);
            if(cur->f(r - 1) <= nw->f(r - 1)) break;
            const T mid = (l + r) / 2;
            if(cur->f(mid) <= nw->f(mid)){
                if(!cur->right){
                    cur->right = new node(*nw);
                    break;
                }else{
                    cur = cur->right, l = mid;
                }
            }else{
                swap(cur, nw);
                if(!cur->left){
                    cur->left = new node(*nw);
                    break;
                }else{
                    cur = cur->left, r = mid;
                }
            }
        }
    }
    T query(node *cur, const T k, T l, T r) const {
        T ans = numeric_limits<T>::max();
        while(cur){
            ans = min(ans, cur->f(k));
            const T mid = (l + r) / 2;
            if(k < mid){
                cur = cur->left, r = mid;
            }else{
                cur = cur->right, l = mid;
            }
        }
        return ans;
    }
    void clear(node *cur){
        if(cur->left) clear(cur->left);
        if(cur->right) clear(cur->right);
        delete cur;
    }
    const T lpos, rpos;
    node *root;
public:
    CHT(const T _lpos, const T _rpos) : lpos(_lpos), rpos(_rpos), root(new node()){
        assert(lpos < rpos);
    }
    // ~CHT(){ clear(root); }
    // f(x) = a * x + b を挿入
    void add_line(const T a, const T b){
        node nw(a, b);
        return _add_line(root, &nw, lpos, rpos);
    }
    // x = k での最小値
    T query(const T k) const {
        return query(root, k, lpos, rpos);
    }
};

int main(){

    ll mx=0, N;
    cin >> N;
    vector<ll> dp(N+1, -1e18), A(N+1);
    for (int i=1; i<=N; i++){
        cin >> A[i];
    }

    CHT<ll> cht(-1e5-1, 1e5+1);
    cht.add_line(0, 0);

    for (int i=1; i<=N; i++){
        dp[i] = cht.query(A[i]);
        cht.add_line(-A[i], dp[i]);
        mx = max(-dp[i], mx);
    }

    cout << mx << endl;

    return 0;
}
0