結果

問題 No.595 登山
ユーザー vjudge1
提出日時 2025-09-05 12:20:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 30 ms / 1,500 ms
コード長 685 bytes
コンパイル時間 1,516 ms
コンパイル使用メモリ 161,964 KB
実行使用メモリ 10,892 KB
最終ジャッジ日時 2025-09-05 12:20:51
合計ジャッジ時間 3,871 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:10:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |     scanf("%d%lld",&n,&p);
      |     ~~~~~^~~~~~~~~~~~~~~~
main.cpp:13:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         scanf("%d",&h[i]);
      |         ~~~~~^~~~~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=2e5+5;
int n;
int h[N];
ll p;
ll f[N][2][2];
int main(){
    scanf("%d%lld",&n,&p);
    memset(f,0x3f,sizeof(f));
    for(int i=1;i<=n;++i){
        scanf("%d",&h[i]);
    }
    f[1][1][0]=0;
    f[1][1][1]=0;
    f[1][0][0]=0;
    f[1][0][1]=0;
    for(int i=2;i<=n;++i){
        f[i][1][0]=min(f[i-1][1][0]+min(p,1ll*max(0,h[i]-h[i-1])),f[i-1][1][1]+p);
        f[i][1][1]=min(f[i-1][0][0],f[i-1][0][1])+p+max(0,h[i-1]-h[i]);
        f[i][0][0]=min(f[i-1][1][0],f[i-1][1][1]);
        f[i][0][1]=min(f[i-1][0][1],f[i-1][0][0])+max(0,h[i-1]-h[i]);
    }
    cout<<min(f[n][1][0],f[n][1][1]);
    return 0;
}
0