結果

問題 No.1297 銅像
ユーザー 👑 tatyamtatyam
提出日時 2020-09-12 20:01:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 2,359 bytes
コンパイル時間 2,355 ms
コンパイル使用メモリ 211,640 KB
実行使用メモリ 19,112 KB
最終ジャッジ日時 2023-08-30 18:51:27
合計ジャッジ時間 5,418 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 150 ms
18,904 KB
testcase_10 AC 82 ms
18,844 KB
testcase_11 AC 167 ms
18,772 KB
testcase_12 AC 134 ms
18,808 KB
testcase_13 AC 111 ms
18,768 KB
testcase_14 AC 113 ms
18,840 KB
testcase_15 AC 112 ms
18,876 KB
testcase_16 AC 118 ms
19,072 KB
testcase_17 AC 137 ms
18,932 KB
testcase_18 AC 140 ms
18,644 KB
testcase_19 AC 116 ms
19,112 KB
testcase_20 AC 166 ms
18,812 KB
testcase_21 AC 165 ms
18,804 KB
testcase_22 AC 119 ms
18,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using pll = pair<ll, ll>;
const ll LINF = 0x1fffffffffffffff;

template< typename T >
struct LiChaoTree {
    struct Line {
        T a, b;

        Line(T a, T b) : a(a), b(b) {}

        inline T get(T x) const { return a * x + b; }

        inline bool over(const Line &b, const T &x) const {
            return get(x) < b.get(x);
        }
    };

    vector< T > xs;
    vector< Line > seg;
    int sz;

    LiChaoTree(const vector< T > &x, T INF) : xs(x) {
        sz = 1;
        while(sz < xs.size()) sz <<= 1;
        while(xs.size() < sz) xs.push_back(xs.back() + 1);
        seg.assign(2 * sz - 1, Line(0, INF));
    }

    void update(Line &x, int k, int l, int r) {
        int mid = (l + r) >> 1;
        auto latte = x.over(seg[k], xs[l]), malta = x.over(seg[k], xs[mid]);
        if(malta) swap(seg[k], x);
        if(l + 1 >= r) return;
        else if(latte != malta) update(x, 2 * k + 1, l, mid);
        else update(x, 2 * k + 2, mid, r);
    }

    void update(T a, T b) { // ax+b
        Line l(a, b);
        update(l, 0, 0, sz);
    }

    T query(T x) { // xs[k]
        int k = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
        k += sz - 1;
        T ret = seg[k].get(x);
        while(k > 0) {
            k = (k - 1) >> 1;
            ret = min(ret, seg[k].get(x));
        }
        return ret;
    }
};
signed main(){
    ll n, C;
    cin >> n >> C;
    vector<pll> a(n);
    for(pll& i : a) cin >> i.first >> i.second;
    vector<ll> dp1(n+1);
    vector<ll> dp2(n+1);
    dp1[0] = LINF;
    vector<ll> x1(n), x2(n);
    for(ll i = 0; i < n; i++){
        x1[i] = a[i].first + C * (i + 1);
        x2[i] = i + 1;
    }
    sort(x1.begin(), x1.end());
    x1.erase(unique(x1.begin(), x1.end()), x1.end());
    LiChaoTree<ll> cht1(x1, LINF), cht2(x2, LINF);
    for(ll i = 0; i < n; i++){
        const auto [A, B] = a[i];
        cht1.update(-i, dp2[i] + C * i * (i + 1) / 2);
        dp1[i + 1] = cht1.query(A + C * (i + 1)) + A * (i + 1) + B + C * i * (i + 1) / 2;
        if(i){
            cht2.update(a[i - 1].first - C * i, dp1[i] - a[i - 1].first * i + C * i * (i - 1) / 2);
            dp2[i + 1] = min(dp1[i + 1], cht2.query(i + 1) + C * (i + 1) * (i + 2) / 2);
        }
        else dp2[i + 1] = dp1[i + 1];
    }
    cout << dp2[n] << endl;
}
0