結果

問題 No.595 登山
ユーザー tnakao0123tnakao0123
提出日時 2017-11-13 16:48:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 28 ms / 1,500 ms
コード長 1,212 bytes
コンパイル時間 793 ms
コンパイル使用メモリ 83,508 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-05-03 20:26:56
合計ジャッジ時間 2,340 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 25 ms
7,424 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 22 ms
6,912 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 22 ms
6,784 KB
testcase_10 AC 17 ms
6,144 KB
testcase_11 AC 14 ms
5,760 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 21 ms
7,552 KB
testcase_15 AC 22 ms
7,508 KB
testcase_16 AC 20 ms
7,384 KB
testcase_17 AC 20 ms
7,512 KB
testcase_18 AC 21 ms
7,508 KB
testcase_19 AC 28 ms
7,552 KB
testcase_20 AC 28 ms
7,388 KB
testcase_21 AC 27 ms
7,424 KB
testcase_22 AC 28 ms
7,516 KB
testcase_23 AC 28 ms
7,424 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 28 ms
7,508 KB
testcase_27 AC 24 ms
7,372 KB
testcase_28 AC 23 ms
7,496 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:50:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |   scanf("%d%d", &n, &p);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:52:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |   for (int i = 0; i < n; i++) scanf("%d", &hs[i]);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 595.cc: No.595 登山 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

int hs[MAX_N];
ll dpr[MAX_N], dpl[MAX_N];

/* subroutines */

inline ll minll(ll a, ll b) { return (a < b) ? a : b; }
inline ll maxll(ll a, ll b) { return (a > b) ? a : b; }
inline void setmin(ll &a, ll b) { if (a > b) a = b; }

/* main */

int main() {
  int n, p;
  scanf("%d%d", &n, &p);

  for (int i = 0; i < n; i++) scanf("%d", &hs[i]);

  dpr[0] = 0;
  dpl[0] = p;
  for (int i = 1; i < n; i++) {
    ll dp0 = min(dpr[i - 1], dpl[i - 1]) + p;
    dpr[i] = min(dp0, dpr[i - 1] + max(0, hs[i] - hs[i - 1]));
    dpl[i] = min(dp0, dpl[i - 1] + max(0, hs[i - 1] - hs[i]));
    //printf("%d: dpr=%lld, dpl=%lld\n", i, dpr[i], dpl[i]);
  }

  printf("%lld\n", min(dpr[n - 1], dpl[n - 1]));
  return 0;
}
0