結果

問題 No.595 登山
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-11-12 07:07:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,213 bytes
コンパイル時間 1,604 ms
コンパイル使用メモリ 163,988 KB
実行使用メモリ 8,212 KB
最終ジャッジ日時 2023-08-16 10:13:24
合計ジャッジ時間 4,953 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,464 KB
testcase_01 AC 3 ms
7,492 KB
testcase_02 AC 3 ms
7,492 KB
testcase_03 AC 3 ms
7,516 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 4 ms
7,436 KB
testcase_25 AC 3 ms
7,476 KB
testcase_26 WA -
testcase_27 AC 22 ms
8,060 KB
testcase_28 AC 22 ms
8,056 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] + 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] + 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