結果

問題 No.595 登山
ユーザー rpy3cpprpy3cpp
提出日時 2017-11-13 01:29:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 1,500 ms
コード長 949 bytes
コンパイル時間 3,784 ms
コンパイル使用メモリ 165,920 KB
実行使用メモリ 4,796 KB
最終ジャッジ日時 2023-08-16 11:06:11
合計ジャッジ時間 3,804 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 23 ms
4,608 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 20 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 20 ms
4,384 KB
testcase_10 AC 14 ms
4,380 KB
testcase_11 AC 12 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 17 ms
4,624 KB
testcase_15 AC 17 ms
4,796 KB
testcase_16 AC 18 ms
4,600 KB
testcase_17 AC 17 ms
4,556 KB
testcase_18 AC 17 ms
4,612 KB
testcase_19 AC 27 ms
4,796 KB
testcase_20 AC 27 ms
4,616 KB
testcase_21 AC 27 ms
4,564 KB
testcase_22 AC 27 ms
4,632 KB
testcase_23 AC 28 ms
4,484 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 28 ms
4,548 KB
testcase_27 AC 21 ms
4,608 KB
testcase_28 AC 21 ms
4,608 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long solve(int N, long long P, const vector<long long> & Hs){
    long long v0 = P; // 末尾にワープして移動して終了
    long long v1 = P; // 末尾にワープして移動して右に移動
    long long v2 = 0; // 右となりから末尾に移動して終了
    for (int i = 1; i < N; ++i){
        long long nv0 = min(min(v0, v1), v2) + P;
        long long nv1 = min(nv0, min(v0, v1) + min(max(0LL, Hs[i - 1] - Hs[i]), P));
        long long nv2 = min(v0, v2) + max(0LL, Hs[i] - Hs[i - 1]);
        v0 = nv0;
        v1 = nv1;
        v2 = nv2;
//        cout << i << ' ' << v0 << ' ' << v1 << ' ' << v2 << endl;
    }
    return min(min(v0, v1), v2);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    long long P;
    cin >> N >> P;
    vector<long long> Hs(N);
    for (auto & h : Hs) cin >> h;
    cout << solve(N, P, Hs) << endl;
    return 0;
}
0