結果

問題 No.595 登山
ユーザー T1610T1610
提出日時 2020-07-13 16:17:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 102 ms / 1,500 ms
コード長 2,079 bytes
コンパイル時間 1,984 ms
コンパイル使用メモリ 199,828 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-25 08:57:39
合計ジャッジ時間 5,146 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 73 ms
16,640 KB
testcase_05 AC 22 ms
7,040 KB
testcase_06 AC 64 ms
14,848 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 12 ms
5,376 KB
testcase_09 AC 64 ms
15,104 KB
testcase_10 AC 45 ms
11,392 KB
testcase_11 AC 40 ms
10,368 KB
testcase_12 AC 8 ms
5,376 KB
testcase_13 AC 21 ms
6,784 KB
testcase_14 AC 55 ms
17,280 KB
testcase_15 AC 57 ms
17,408 KB
testcase_16 AC 58 ms
17,280 KB
testcase_17 AC 56 ms
17,280 KB
testcase_18 AC 55 ms
17,280 KB
testcase_19 AC 97 ms
17,280 KB
testcase_20 AC 98 ms
17,280 KB
testcase_21 AC 97 ms
17,280 KB
testcase_22 AC 95 ms
17,280 KB
testcase_23 AC 99 ms
17,408 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 102 ms
17,280 KB
testcase_27 AC 78 ms
17,280 KB
testcase_28 AC 79 ms
17,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define dump(x) cout << #x << " : " << x << " "
#define dumpL(x) cout << #x << " : " << x << '\n'
#define LINE cout << "line : " << __LINE__ << " "
#define LINEL cout << "line : " << __LINE__ << '\n'
#define dumpV(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << " "
#define dumpVL(v) cout << #v << " : ["; for(auto& t : v) cout << t << ", "; cout<<"]" << endl
#define STOP assert(false)
#else
#define dump(x) 
#define dumpL(x) 
#define LINE 
#define LINEL 
#define dumpV(v) 
#define dumpVL(v) 
#define STOP assert(false)
#endif
#define mp make_pair
 
namespace std {
template<class S, class T>
ostream &operator <<(ostream& out, const pair<S, T>& a) {
    out << '(' << a.fi << ", " << a.se << ')';
    return out;
}
}

int main(){
    ll n, p;
    cin >> n >> p;
    vl h(n);
    rep(i, n) cin >> h[i];
    vl left(n);
    iota(all(left), 0);
    REP(i, 1, n) if(h[i-1] <= h[i]) left[i] = left[i-1];
    vector<vl> dp(n, vl(2,INF));
    dp[0][0] = 0LL;
    REP(i, 1, n) {
        chmin(dp[i][0], dp[i-1][0]+min(p, max(0LL, h[i]- h[i-1])));
        chmin(dp[i][0], dp[i-1][1]+p);
        chmin(dp[i][1], dp[i-1][1]+min(p, max(0LL, h[i-1]- h[i])));
        {
            int j = left[i]-1;
            if(j-1<0) j = 0;
            chmin(dp[i][1], min(dp[j][0], dp[j][1])+p);
        }
    }
    cout << min(dp.back().front(), dp.back().back()) << '\n';
    // rep(i, n) {
    //     cout << " " << dp[i][0] << " " << dp[i][1] << '\n';
    // }
    return 0;
}
0