結果
| 問題 | No.595 登山 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-11-12 11:12:22 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 33 ms / 1,500 ms | 
| コード長 | 755 bytes | 
| コンパイル時間 | 445 ms | 
| コンパイル使用メモリ | 47,488 KB | 
| 最終ジャッジ日時 | 2025-01-05 04:01:49 | 
| ジャッジサーバーID (参考情報) | judge5 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 25 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:11:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   11 |     int n, p; scanf("%d%d", &n, &p);
      |               ~~~~~^~~~~~~~~~~~~~~~
main.cpp:12:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   12 |     vector<int> h(n); repeat (i, n) scanf("%d", &h[i]);
      |                                     ~~~~~^~~~~~~~~~~~~
            
            ソースコード
#include <cstdio>
#include <vector>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }
constexpr ll inf = ll(1e18)+9;
int main() {
    // input
    int n, p; scanf("%d%d", &n, &p);
    vector<int> h(n); repeat (i, n) scanf("%d", &h[i]);
    // solve
    ll fwd = 0;
    ll bck = inf;
    repeat (i, n - 1) {
        ll prv = min(fwd, bck);
        if (h[i] < h[i + 1]) {
            fwd += h[i + 1] - h[i];
        } else {
            bck += h[i] - h[i + 1];
        }
        setmin(fwd, prv + p);
        setmin(bck, prv + p);
    }
    ll result = min(fwd, bck);
    // output
    printf("%lld\n", result);
    return 0;
}
            
            
            
        