結果

問題 No.595 登山
ユーザー beetbeet
提出日時 2017-11-11 00:14:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 536 ms / 1,500 ms
コード長 2,701 bytes
コンパイル時間 1,615 ms
コンパイル使用メモリ 171,400 KB
実行使用メモリ 22,816 KB
最終ジャッジ日時 2024-05-03 15:00:01
合計ジャッジ時間 11,242 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 482 ms
22,656 KB
testcase_05 AC 129 ms
8,320 KB
testcase_06 AC 414 ms
22,020 KB
testcase_07 AC 39 ms
6,940 KB
testcase_08 AC 60 ms
6,944 KB
testcase_09 AC 413 ms
22,192 KB
testcase_10 AC 277 ms
13,268 KB
testcase_11 AC 245 ms
12,908 KB
testcase_12 AC 29 ms
6,940 KB
testcase_13 AC 113 ms
8,136 KB
testcase_14 AC 482 ms
22,692 KB
testcase_15 AC 481 ms
22,816 KB
testcase_16 AC 484 ms
22,708 KB
testcase_17 AC 480 ms
22,716 KB
testcase_18 AC 482 ms
22,592 KB
testcase_19 AC 528 ms
22,660 KB
testcase_20 AC 536 ms
22,800 KB
testcase_21 AC 524 ms
22,740 KB
testcase_22 AC 530 ms
22,656 KB
testcase_23 AC 525 ms
22,740 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 529 ms
22,680 KB
testcase_27 AC 512 ms
22,700 KB
testcase_28 AC 509 ms
22,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;

template <typename T,typename E>
struct SegmentTree{
  typedef function<T(T,T)> F;
  typedef function<T(T,E)> G;
  typedef function<E(E,E)> H;
  typedef function<E(E,Int)> P;
  Int n;
  F f;
  G g;
  H h;
  P p;
  T d1;
  E d0;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(Int n_,F f,G g,H h,T d1,E d0,
	      vector<T> v=vector<T>(),P p=[](E a,Int b){return a;}):
    f(f),g(g),h(h),d1(d1),d0(d0),p(p){
    init(n_);
    if(n_==(Int)v.size()) build(n_,v);
  }
  void init(Int n_){
    n=1;
    while(n<n_) n*=2;
    dat.clear();
    dat.resize(2*n-1,d1);
    laz.clear();
    laz.resize(2*n-1,d0);
  }
  void build(Int n_, vector<T> v){
    for(Int i=0;i<n_;i++) dat[i+n-1]=v[i];
    for(Int i=n-2;i>=0;i--)
      dat[i]=f(dat[i*2+1],dat[i*2+2]);
  }
  inline void eval(Int len,Int k){
    if(laz[k]==d0) return;
    if(k*2+1<n*2-1){
      laz[k*2+1]=h(laz[k*2+1],laz[k]);
      laz[k*2+2]=h(laz[k*2+2],laz[k]);
    }
    dat[k]=g(dat[k],p(laz[k],len));
    laz[k]=d0;
  }
  T update(Int a,Int b,E x,Int k,Int l,Int r){
    eval(r-l,k);
    if(r<=a||b<=l) return dat[k];
    if(a<=l&&r<=b){
      laz[k]=h(laz[k],x);
      return g(dat[k],p(laz[k],r-l));
    }
    return dat[k]=f(update(a,b,x,k*2+1,l,(l+r)/2),
		    update(a,b,x,k*2+2,(l+r)/2,r));
  }
  T update(Int a,Int b,E x){
    return update(a,b,x,0,0,n);
  }
  T query(Int a,Int b,Int k,Int l,Int r){
    eval(r-l,k);
    if(r<=a||b<=l) return d1;
    if(a<=l&&r<=b) return dat[k];
    T vl=query(a,b,k*2+1,l,(l+r)/2);
    T vr=query(a,b,k*2+2,(l+r)/2,r);
    return f(vl,vr);
  }
  T query(Int a,Int b){
    return query(a,b,0,0,n);
  }
};


const Int INF=1LL<<55LL;
signed main(){
  Int n,p;
  cin>>n>>p;
  vector<Int> h(n+1,INF);
  for(Int i=0;i<n;i++) cin>>h[i];
  SegmentTree<Int,Int> ushi(n+1,
			    [](Int a,Int b){
			      return min(a,b);
			    },
			    [](Int a,Int b){
			      return a+b;
			    },
			    [](Int a,Int b){
			      return a+b;
			    },
			    INF,Int(0),vector<Int>(n+1,0));
  SegmentTree<Int,Int> beet=ushi;
  
  vector<Int> dp(n,INF);
  dp[0]=0;
  ushi.update(1,2,p);
  for(Int i=1;i<n;i++){
    ushi.update(0,i,max(Int(0),h[i]-h[i-1]));
    beet.update(0,i,max(Int(0),h[i-1]-h[i]));
    
    dp[i]=min(dp[i],ushi.query(0,i));
    dp[i]=min(dp[i],beet.query(0,i)+p);

    ushi.update(i+1,i+2,dp[i]+p);
    beet.update(i+1,i+2,dp[i]);

    /*/
    cout<<i<<":"<<dp[i]<<endl;
    cout<<"ushi::"<<endl;
    for(int j=0;j<n;j++){
      cout<<j<<":"<<ushi.query(j,j+1)<<endl;
    }
    cout<<"beet::"<<endl;
    for(int j=0;j<n;j++){
      cout<<j<<":"<<beet.query(j,j+1)<<endl;
    }
    //*/
  }
  cout<<dp.back()<<endl;
  return 0;
}
0