結果

問題 No.704 ゴミ拾い Medium
ユーザー parukiparuki
提出日時 2018-09-25 16:25:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 198 ms / 1,500 ms
コード長 2,409 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 170,596 KB
実行使用メモリ 23,224 KB
最終ジャッジ日時 2024-04-15 06:51:11
合計ジャッジ時間 8,129 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 196 ms
23,160 KB
testcase_25 AC 197 ms
23,200 KB
testcase_26 AC 197 ms
23,204 KB
testcase_27 AC 197 ms
23,224 KB
testcase_28 AC 197 ms
23,060 KB
testcase_29 AC 197 ms
23,172 KB
testcase_30 AC 198 ms
22,984 KB
testcase_31 AC 196 ms
23,000 KB
testcase_32 AC 197 ms
23,064 KB
testcase_33 AC 197 ms
23,172 KB
testcase_34 AC 162 ms
23,160 KB
testcase_35 AC 164 ms
23,060 KB
testcase_36 AC 160 ms
23,100 KB
testcase_37 AC 163 ms
23,216 KB
testcase_38 AC 164 ms
23,108 KB
testcase_39 AC 163 ms
23,160 KB
testcase_40 AC 165 ms
23,172 KB
testcase_41 AC 164 ms
23,180 KB
testcase_42 AC 165 ms
23,076 KB
testcase_43 AC 164 ms
23,144 KB
testcase_44 AC 2 ms
6,944 KB
testcase_45 AC 2 ms
6,940 KB
testcase_46 AC 166 ms
23,144 KB
testcase_47 AC 179 ms
23,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;

template<class Val, class Cmp>
class DynamicRMQ {
    int n;
    Val init;
    vector<Val> dat;
    Cmp cmp;
    inline Val query(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) return init;
        if (a <= l && r <= b) return dat[k];
        else {
            Val vl, vr;
            vl = query(a, b, k << 1, l, (l + r) >> 1);
            vr = query(a, b, (k << 1) | 1, (l + r) >> 1, r);
            return cmp(vl, vr) ? vl : vr;
        }
    }
public:
    DynamicRMQ() {}
    DynamicRMQ(int n_, Val init_) :n(1), init(init_) {
        for (; n<n_; n <<= 1);
        dat = vector<Val>(n << 1, init);
    }
    void update(int k, Val a) {
        k += n;
        dat[k] = a;
        while (k > 1) {
            k >>= 1;
            dat[k] = cmp(dat[k << 1], dat[(k << 1) | 1]) ? dat[k << 1] : dat[(k << 1) | 1];
        }
    }
    Val query(int a, int b) {
        return query(a, b, 1, 0, n);
    }
};

typedef DynamicRMQ<long long, less<long long> > RMinQ64;
typedef DynamicRMQ<long long, greater<long long> > RMaxQ64;
typedef DynamicRMQ<int, less<int> > RMinQ32;
typedef DynamicRMQ<int, greater<int> > RMaxQ32;

const int N_MAX = 300005;
const ll INF = LLONG_MAX / 3;
int N, A[N_MAX], X[N_MAX], Y[N_MAX];

void solve() {
    cin >> N;
    for (int *a : { A,X,Y }) rep(i, N)cin >> a[i];
    RMinQ64 lef(N + 1, INF), rig(N + 1, INF);
    lef.update(0, -X[0]+Y[0]);
    rig.update(0, X[0] + Y[0]);
    ll ans = -1;
    rep(i, N) {
        int m = upper_bound(X, X + N, A[i]) - X;
        ll z = ans = min(lef.query(0, m) + A[i], rig.query(m, N + 1) - A[i]);
        lef.update(i + 1, z - X[i + 1] + Y[i + 1]);
        rig.update(i + 1, z + X[i + 1] + Y[i + 1]);
    }
    cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0