結果

問題 No.595 登山
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-11-12 07:09:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 1,500 ms
コード長 4,229 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 165,296 KB
実行使用メモリ 8,204 KB
最終ジャッジ日時 2023-08-16 10:19:28
合計ジャッジ時間 3,994 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,640 KB
testcase_01 AC 4 ms
7,640 KB
testcase_02 AC 4 ms
7,508 KB
testcase_03 AC 4 ms
7,760 KB
testcase_04 AC 23 ms
8,068 KB
testcase_05 AC 9 ms
7,560 KB
testcase_06 AC 20 ms
7,816 KB
testcase_07 AC 5 ms
7,512 KB
testcase_08 AC 6 ms
7,464 KB
testcase_09 AC 21 ms
7,908 KB
testcase_10 AC 15 ms
7,516 KB
testcase_11 AC 14 ms
7,456 KB
testcase_12 AC 5 ms
7,464 KB
testcase_13 AC 8 ms
7,444 KB
testcase_14 AC 18 ms
8,132 KB
testcase_15 AC 18 ms
8,204 KB
testcase_16 AC 17 ms
8,112 KB
testcase_17 AC 18 ms
8,080 KB
testcase_18 AC 18 ms
8,184 KB
testcase_19 AC 28 ms
8,092 KB
testcase_20 AC 29 ms
8,128 KB
testcase_21 AC 28 ms
8,132 KB
testcase_22 AC 28 ms
8,200 KB
testcase_23 AC 28 ms
8,072 KB
testcase_24 AC 4 ms
7,464 KB
testcase_25 AC 4 ms
7,460 KB
testcase_26 AC 29 ms
8,148 KB
testcase_27 AC 22 ms
8,128 KB
testcase_28 AC 22 ms
8,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
struct BitTrie {
    struct Node { int cnt, val; Node* c[2]; Node() { cnt = 0; val = 0; c[0] = c[1] = 0; } };
    Node* root; BitTrie() { root = new Node(); }
    void add(int x, int d = 1) { update(root, 31, x, d); }
    void update(Node*& x, int dig, int a, int d) {
        if (!x) x = new Node(); x->cnt += d;
        if (dig == -1) { x->val = a; return; } update(x->c[(a&(1 << dig))>0], dig - 1, a, d);
    }
    void dump() { dump(root); }
    void dump(Node*& x) {
        printf("[%d,%d,%d] (", x, x->cnt, x->val);
        if (x->c[0]) printf("%d) (", x->c[0]);
        else printf("##) (");
        if (x->c[1]) printf("%d)\n", x->c[1]);
        else printf("##)\n");

        if (x->c[0]) dump(x->c[0]);
        if (x->c[1]) dump(x->c[1]);
    }




    // get the minimal value that (x xor a) : x is one of all numbers
    // warning!! returned value is (x xor a)
    int inf = INT_MAX / 2;
    int getMinXorA(Node* n, int dig, int a) {
        if (dig < 0) {
            assert(n != NULL);
            return n->val ^ a;
        }

        int d = (a & (1 << dig)) > 0;
        int dd = 1 - d;
        
        if (n->c[d]) if (n->c[d]->cnt) return getMinXorA(n->c[d], dig - 1, a);
        return getMinXorA(n->c[dd], dig - 1, a);
    }
    int getMinXorA(int a) { return getMinXorA(root, 31, a); }

    // get the number of the variables satisfying that (x xor a) < k
    int countStrictlyLessThanKXorA(Node* n, int dig, int a, int k) {
        if (dig < 0) return 0;

        int d = (a & (1 << dig)) > 0;
        int dd = 1 - d;

        int f = (k & (1 << dig)) > 0;
        int res = 0;
        if (f == 1) {
            if (n->c[dd]) res += countStrictlyLessThanKXorA(n->c[dd], dig - 1, a, k);
            if (n->c[d]) res += n->c[d]->cnt;
        }
        else {
            if (n->c[d]) res += countStrictlyLessThanKXorA(n->c[d], dig - 1, a, k);
        }
        return res;
    }
    int countStrictlyLessThanKXorA(int a, int k) { return countStrictlyLessThanKXorA(root, 31, a, k); }


    // get the number of the variables satisfying that k <= (x xor a)
    int countGreaterThanKXorA(Node* n, int dig, int a, int k) {
        if (!n) return 0;
        if (dig < 0) return n->cnt;
        int res = 0;
        if (!(k&(1 << dig)) && n->c[!(a&(1 << dig))]) res += n->c[!(a&(1 << dig))]->cnt;
        return res + countGreaterThanKXorA(n->c[((k^a)&(1 << dig))>0], dig - 1, a, k);
    }
    int countGreaterThanKXorA(int a, int k) { return countGreaterThanKXorA(root, 31, a, k); }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/



typedef long long ll;
#define INF 1LL<<60
int N; ll P, H[201010];
ll dp[201010][2];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> P;
    rep(i, 0, N) cin >> H[i];

    rep(i, 0, 201010) rep(dir, 0, 2) dp[i][dir] = INF;
    dp[0][0] = 0;

    rep(i, 0, N - 1) {
        // ->を伸ばす遷移
        dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + min(P, max(H[i + 1] - H[i], 0LL)));
        
        // 今まで->だったのを<-とする遷移
        dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + P);

        // <-を伸ばす遷移
        dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + min(P, max(H[i] - H[i + 1], 0LL)));

        // 今まで<-だったのを->とする遷移
        dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + P);
    }

    ll ans = min(dp[N - 1][0], dp[N - 1][1]);
    cout << ans << endl;
}
0