結果

問題 No.704 ゴミ拾い Medium
ユーザー 夕叢霧香(ゆうむらきりか)
提出日時 2018-01-28 12:55:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 121 ms / 1,500 ms
コード長 1,109 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 71,324 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-12-29 12:33:24
合計ジャッジ時間 6,892 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
#define rep(i,n)for(int i=0;i<(int)(n);++i)

struct MinBIT {
  static const int SIZE = 1 << 19;
  static const int INFINITY = (1 << 30) - 1;
  int bit[SIZE];
  MinBIT() {
    rep(i,SIZE)bit[i]=INFINITY;
  }
  void update(int x, int value) {
    while(x<SIZE){
      bit[x]=min(bit[x],value);
      x+=x&(-x);
    }
  }
  int query(int x) const {
    int y = INFINITY;
    while(x>0){
      y=min(y,bit[x]);
      x&=x-1;
    }
    return y;
  }
};


int a[300001],x[300001],y[300001];
int dp[300001];
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin>>n;
  rep(i,n)cin>>a[i];
  rep(i,n)cin>>x[i];
  rep(i,n)cin>>y[i];
  MinBIT m1;
  MinBIT p1;
  rep(i,n){
    // update
    m1.update(300001-x[i],x[i]+y[i]+dp[i]);
    p1.update(1+x[i],-x[i]+y[i]+dp[i]);
    // query
    int ans=1e9;
    int curA=a[i];
    ans=min(ans,m1.query(300001-curA)-curA);
    ans=min(ans,p1.query(curA+1)+curA);
    dp[i+1]=ans;
  }
  cout<<dp[n]<<endl;
}
0