結果

問題 No.704 ゴミ拾い Medium
ユーザー ikdikd
提出日時 2018-06-16 02:16:07
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,618 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 78,560 KB
実行使用メモリ 16,064 KB
最終ジャッジ日時 2023-09-13 05:46:03
合計ジャッジ時間 8,991 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 WA -
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 WA -
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 274 ms
15,800 KB
testcase_25 WA -
testcase_26 AC 276 ms
15,780 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 274 ms
15,804 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 275 ms
15,944 KB
testcase_34 AC 235 ms
16,056 KB
testcase_35 AC 236 ms
15,820 KB
testcase_36 AC 236 ms
15,868 KB
testcase_37 AC 236 ms
15,984 KB
testcase_38 AC 239 ms
15,836 KB
testcase_39 AC 236 ms
15,972 KB
testcase_40 AC 236 ms
15,980 KB
testcase_41 AC 236 ms
15,892 KB
testcase_42 AC 237 ms
15,868 KB
testcase_43 AC 235 ms
15,916 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 300 ms
15,860 KB
testcase_47 AC 288 ms
15,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)

struct SegmentTree{
  int n=1, inf=1e8;
  vector<int> seg;
  SegmentTree(int m){
    while(n<m) n*=2;
    seg.resize(n*2-1, inf);
  }
  void update(int i, int x){
    i+=n-1;
    seg[i]=x;
    while(i>0){
      i=(i-1)/2;
      seg[i]=min(seg[i*2+1], seg[i*2+2]);
    }
  }
  int getmin(int ql, int qr){
    return getmin(ql, qr, 0, 0, n);
  }
  int getmin(int ql, int qr, int i, int il, int ir){
    if(qr<=il || ir<=ql) return inf;
    if(il<=ql && qr<=ir) return seg[i];
    int m=(ir+il)/2;
    int vl=getmin(ql, qr, i*2+1, il, m),
        vr=getmin(ql, qr, i*2+1, m, ir);
    return min(vl, vr);
  }
};

void chmin(int &l, int r){if(l>r)l=r;}

int main(){

  int n; cin>> n;
  vector<int> a(n+1);
  rep(i, n) cin>> a[i+1];
  vector<int> x(n+1);
  rep(i, n) cin>> x[i+1];
  vector<int> y(n+1);
  rep(i, n) cin>> y[i+1];

  const int inf=1e8;
  SegmentTree seg_l(n), seg_r(n);

  vector<int> dp(n+1, inf);
  dp[0]=0;
  for(int i=1, j=1; i<=n; i++){ // j := 人(i-1)より右にあるゴミで最も近いもののindex
    chmin(dp[i], dp[i-1]+abs(a[i]-x[i])+y[i]);
    chmin(dp[i], seg_l.getmin(0, i)+a[i]); // seg_l <- -x+y+dp[i]が入ってる
    chmin(dp[i], seg_r.getmin(0, i)-a[i]); // seg_r <-  x+y+dp[i]が入ってる
    seg_r.update(j-1, x[i]+y[i]+dp[i-1]);
    while(j<=i && x[j]<=a[i]){
      // 人(i-1)と人iの間にあったゴミたちを...
      seg_l.update(j-1, -x[j]+y[j]+dp[j-1]);
      seg_r.update(j-1, inf);
      j++;
    }
  }

  cout<< dp[n]<< endl;

  return 0;
}
0