結果

問題 No.595 登山
ユーザー ryoissyryoissy
提出日時 2017-11-10 23:25:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 664 ms / 1,500 ms
コード長 1,547 bytes
コンパイル時間 1,886 ms
コンパイル使用メモリ 153,448 KB
実行使用メモリ 29,464 KB
最終ジャッジ日時 2023-08-16 05:27:03
合計ジャッジ時間 14,583 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
26,480 KB
testcase_01 AC 7 ms
26,596 KB
testcase_02 AC 7 ms
26,680 KB
testcase_03 AC 7 ms
26,544 KB
testcase_04 AC 626 ms
29,124 KB
testcase_05 AC 165 ms
26,984 KB
testcase_06 AC 500 ms
28,660 KB
testcase_07 AC 61 ms
26,684 KB
testcase_08 AC 92 ms
26,720 KB
testcase_09 AC 476 ms
28,784 KB
testcase_10 AC 357 ms
27,880 KB
testcase_11 AC 343 ms
27,632 KB
testcase_12 AC 54 ms
26,596 KB
testcase_13 AC 167 ms
26,932 KB
testcase_14 AC 646 ms
29,212 KB
testcase_15 AC 572 ms
29,464 KB
testcase_16 AC 605 ms
29,252 KB
testcase_17 AC 639 ms
29,208 KB
testcase_18 AC 646 ms
29,464 KB
testcase_19 AC 576 ms
29,212 KB
testcase_20 AC 617 ms
29,128 KB
testcase_21 AC 656 ms
29,224 KB
testcase_22 AC 659 ms
29,420 KB
testcase_23 AC 664 ms
29,264 KB
testcase_24 AC 7 ms
26,572 KB
testcase_25 AC 8 ms
26,556 KB
testcase_26 AC 621 ms
29,140 KB
testcase_27 AC 650 ms
29,264 KB
testcase_28 AC 650 ms
29,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define INF 100000000000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class segtree{
public:
	static const int N=1<<18;
	ll dp[1<<19];
	ll lazy[1<<19];
	segtree(){
		memset(dp,0,sizeof(dp));
	}
	void lazy_eval(int k){
		dp[k]+=lazy[k];
		if(k<N-1){
			lazy[k*2+1]+=lazy[k];
			lazy[k*2+2]+=lazy[k];
		}
		lazy[k]=0;
	}
	void lazy_upd(int k){
		dp[k]=min(dp[k*2+1],dp[k*2+2]);
	}

	void update(int a,int b,ll v,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return;
		if(a<=l && r<=b){
			lazy[k]+=v;
			lazy_eval(k);
			return;
		}
		int mid=(l+r)/2;
		update(a,b,v,k*2+1,l,mid);
		update(a,b,v,k*2+2,mid,r);
		lazy_upd(k);
	}

	ll query(int a,int b,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return INF;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		ll vl=query(a,b,k*2+1,l,mid);
		ll vr=query(a,b,k*2+2,mid,r);
		lazy_upd(k);
		return min(vl,vr);
	}
};

segtree seg[3];

int n;
ll p;
ll h[200005];


int main(void){
	scanf("%d%lld",&n,&p);
	for(int i=0;i<n;i++){
		scanf("%lld",&h[i]);
	}
	for(int i=1;i<n;i++){
		ll val=h[i]-h[i-1];
		if(val>0){
			seg[0].update(0,i,val);
			seg[2].update(0,i-1,val);
		}else{
			seg[1].update(0,i-1,-val);
		}
		ll bv=seg[0].query(0,i);
		bv=min(bv,seg[2].query(0,i)+p);
		seg[1].update(i,i+1,min(seg[1].query(0,i)+p,bv));
		seg[2].update(i,i+1,min(seg[1].query(0,i)+p,bv));
		seg[0].update(i,i+1,bv);
	}
	ll ans=INF;
	for(int i=0;i<3;i++){
		ans=min(ans,seg[i].query(n-1,n));
	}
	printf("%lld\n",ans);
	return 0;
}
0