結果

問題 No.595 登山
ユーザー TAISA_TAISA_
提出日時 2019-12-15 22:42:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,976 bytes
コンパイル時間 1,682 ms
コンパイル使用メモリ 173,180 KB
実行使用メモリ 21,228 KB
最終ジャッジ日時 2023-09-15 15:50:41
合計ジャッジ時間 9,125 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 WA -
testcase_27 AC 278 ms
20,976 KB
testcase_28 AC 277 ms
20,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr ll MOD = 1e9 + 7;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T, typename E> struct Segtree {
    inline T merge(const T &a, const T &b) { return min(a, b); }
    inline void act(E &a, const E &b) { a += b; }
    inline void comb(T &a, const E &b) { a += b; }
    int n;
    T et;
    E ee;
    vector<T> dat;
    vector<E> laz;
    Segtree(int n_, T et, E ee) : et(et), ee(ee) {
        n = 1;
        while (n < n_) {
            n <<= 1;
        }
        dat.resize(2 * n, et);
        laz.resize(2 * n, ee);
    }
    inline void eval(int k) {
        if (laz[k] == ee) {
            return;
        }
        comb(dat[k], laz[k]);
        if (k < n) {
            act(laz[k << 1], laz[k]);
            act(laz[k << 1 | 1], laz[k]);
        }
        laz[k] = ee;
    }
    void upd(const int &a, const int &b, const E &x, int k, int l, int r) {
        eval(k);
        if (b <= l || r <= a) {
            return;
        }
        if (a <= l && r <= b) {
            act(laz[k], x);
            eval(k);
            return;
        }
        upd(a, b, x, k << 1, l, (l + r) >> 1);
        upd(a, b, x, k << 1 | 1, (l + r) >> 1, r);
        dat[k] = merge(dat[k << 1], dat[k << 1 | 1]);
    }
    inline void upd(const int &a, const int &b, const E &x) {
        if (a >= b) {
            return;
        }
        upd(a, b, x, 1, 0, n);
    }
    T get(const int &a, const int &b, int k, int l, int r) {
        if (b <= l || r <= a) {
            return et;
        }
        eval(k);
        if (a <= l && r <= b) {
            return dat[k];
        }
        return merge(get(a, b, k << 1, l, (l + r) >> 1),
                     get(a, b, k << 1 | 1, (l + r) >> 1, r));
    }
    inline T get(const int &a, const int &b) {
        if (a >= b) {
            return et;
        }
        return get(a, b, 1, 0, n);
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, p;
    cin >> n >> p;
    vector<ll> h(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    Segtree<ll, ll> seg0(n + 10, LINF, 0), seg1(n + 10, LINF, 0);
    seg0.upd(0, 1, -LINF - p);
    seg1.upd(0, 1, -LINF);
    for (int i = 0; i < n; i++) {
        if (i > 0) {
            seg0.upd(0, i + 1, max(0LL, h[i] - h[i - 1]));
            seg1.upd(0, i + 1, max(0LL, h[i - 1] - h[i]));
        }
        ll dp0 = seg1.get(0, i + 1) + p;
        ll dp1 = seg0.get(0, i + 1) + p;
        // cout << i << " " << dp0 << " " << dp1 << endl;
        if (i == n - 1) {
            cout << min(dp0, dp1) << endl;
            return 0;
        }
        seg0.upd(i + 1, i + 2, -LINF + dp0);
        seg1.upd(i + 1, i + 2, -LINF + dp1);
    }
}
0