結果

問題 No.595 登山
ユーザー 👑 kmjpkmjp
提出日時 2017-11-10 23:58:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 29 ms / 1,500 ms
コード長 1,337 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 146,380 KB
実行使用メモリ 9,776 KB
最終ジャッジ日時 2023-08-16 05:45:17
合計ジャッジ時間 3,557 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,524 KB
testcase_01 AC 3 ms
7,472 KB
testcase_02 AC 2 ms
7,464 KB
testcase_03 AC 2 ms
7,536 KB
testcase_04 AC 22 ms
9,516 KB
testcase_05 AC 8 ms
8,344 KB
testcase_06 AC 19 ms
9,264 KB
testcase_07 AC 4 ms
7,852 KB
testcase_08 AC 6 ms
7,944 KB
testcase_09 AC 19 ms
9,320 KB
testcase_10 AC 15 ms
8,844 KB
testcase_11 AC 13 ms
8,976 KB
testcase_12 AC 4 ms
7,792 KB
testcase_13 AC 8 ms
8,304 KB
testcase_14 AC 17 ms
9,760 KB
testcase_15 AC 18 ms
9,652 KB
testcase_16 AC 17 ms
9,656 KB
testcase_17 AC 17 ms
9,672 KB
testcase_18 AC 17 ms
9,720 KB
testcase_19 AC 29 ms
9,660 KB
testcase_20 AC 28 ms
9,672 KB
testcase_21 AC 28 ms
9,760 KB
testcase_22 AC 28 ms
9,692 KB
testcase_23 AC 29 ms
9,692 KB
testcase_24 AC 3 ms
7,476 KB
testcase_25 AC 2 ms
7,520 KB
testcase_26 AC 29 ms
9,668 KB
testcase_27 AC 24 ms
9,668 KB
testcase_28 AC 22 ms
9,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
ll P,H[202020];
ll dp[3][202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>P;
	FOR(i,N) cin>>H[i];
	
	dp[0][0]=dp[1][0]=dp[2][0]=0;
	// 0-free 1-non 2-req
	for(i=1;i<N;i++) {
		dp[0][i]=dp[1][i]=dp[2][i]=1LL<<60;
		dp[0][i]=min(dp[0][i],dp[0][i-1]+min(P,max(0LL,H[i]-H[i-1])));
		dp[0][i]=min(dp[0][i],dp[1][i-1]+P);
		dp[1][i]=min(dp[1][i],dp[2][i-1]+P+min(P,max(0LL,H[i-1]-H[i])));
		dp[2][i]=min(dp[2][i],dp[0][i-1]);
		dp[2][i]=min(dp[2][i],dp[1][i-1]);
		dp[2][i]=min(dp[2][i],dp[2][i-1]+min(P,max(0LL,H[i-1]-H[i])));
	}
	
//	FOR(i,N) cout<<dp[0][i]<<" "<<dp[1][i]<<" "<<dp[2][i]<<" "<<endl;
	cout<<min({dp[0][N-1],dp[1][N-1]})<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0