結果

問題 No.704 ゴミ拾い Medium
ユーザー raven7959raven7959
提出日時 2022-05-24 14:51:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 1,500 ms
コード長 6,391 bytes
コンパイル時間 3,421 ms
コンパイル使用メモリ 207,976 KB
実行使用メモリ 28,944 KB
最終ジャッジ日時 2023-10-20 18:49:32
合計ジャッジ時間 11,788 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 139 ms
28,944 KB
testcase_25 AC 148 ms
28,944 KB
testcase_26 AC 148 ms
28,944 KB
testcase_27 AC 148 ms
28,944 KB
testcase_28 AC 150 ms
28,944 KB
testcase_29 AC 147 ms
28,944 KB
testcase_30 AC 149 ms
28,944 KB
testcase_31 AC 150 ms
28,944 KB
testcase_32 AC 142 ms
28,944 KB
testcase_33 AC 144 ms
28,944 KB
testcase_34 AC 131 ms
28,944 KB
testcase_35 AC 139 ms
28,944 KB
testcase_36 AC 138 ms
28,944 KB
testcase_37 AC 132 ms
28,944 KB
testcase_38 AC 135 ms
28,944 KB
testcase_39 AC 128 ms
28,944 KB
testcase_40 AC 131 ms
28,944 KB
testcase_41 AC 135 ms
28,944 KB
testcase_42 AC 138 ms
28,944 KB
testcase_43 AC 134 ms
28,944 KB
testcase_44 AC 2 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 145 ms
28,944 KB
testcase_47 AC 149 ms
28,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n"
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n"
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vld = vector<long double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<long double>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
const int INF = 1e9;
const ll LINF = 2e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
bool ispow2(int i) { return i && (i & -i) == i; }
bool ispow2(ll i) { return i && (i & -i) == i; }
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

static uint32_t RandXor(){
    static uint32_t x=123456789;
    static uint32_t y=362436069;
    static uint32_t z=521288629;
    static uint32_t w=88675123;
    uint32_t t;
 
    t=x^(x<<11);
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

// segtree<S,op,e> seg(int n)またはsegtree<S,op,e> seg(vector<S> vec)で初期化
// Sは型, Sを返す関数op(S a,S b)と単位元S e()を設定する
// set(int p,S x): a[p]にxを代入する
// get(int p): a[p]を取得する
// prod(int l,int r): [l,r)をfoldした結果を返す
// all_prod(): [0,n)をfoldした結果を返す
// max_right<f>(int l): bool f(S x)を渡すとl以降でf(prod(l,r))=trueとなる最大のrを返す
// min_left: max_rightの逆

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template <class S, S (*op)(S, S), S (*e)()>
struct segtree{
public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(vector<S>(n, e())) {}
    segtree(const vector<S> &v) : _n(int(v.size())){
        log = ceil_pow2(_n);
        size = 1 << log;
        d = vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) update(i);
    }

    void set(int p, S x){
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++)
            update(p >> i);
    }

    S get(int p){
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r){
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r){
            if (l & 1)
                sml = op(sml, d[l++]);
            if (r & 1)
                smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)>
    int max_right(int l){
        return max_right(l, [](S x)
                         { return f(x); });
    }
    template <class F>
    int max_right(int l, F f){
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do{
            while (l % 2 == 0)
                l >>= 1;
            if (!f(op(sm, d[l]))){
                while (l < size){
                    l = (2 * l);
                    if (f(op(sm, d[l]))){
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)>
    int min_left(int r){
        return min_left(r, [](S x)
                        { return f(x); });
    }
    template <class F>
    int min_left(int r, F f){
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do{
            r--;
            while (r > 1 && (r % 2))
                r >>= 1;
            if (!f(op(d[r], sm))){
                while (r < size){
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))){
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

ll op(ll l,ll r){return min(l,r);}
ll e(){return LINF;}

void solve(){
    ll N;
    cin>>N;
    vl A(N),X(N),Y(N);
    cin>>A>>X>>Y;
    segtree<ll,op,e> DP1(N+1);
    segtree<ll,op,e> DP2(N+1);
    vl DP(N+1,LINF);
    DP[0]=0;
    DP1.set(0,Y[0]-X[0]);
    DP2.set(0,Y[0]+X[0]);
    for(int i=1;i<=N;i++){
        int p=lower_bound(all(X),A[i-1])-X.begin();
        if(p) chmin(DP[i],DP1.prod(0,min(p-1,i-1)+1)+A[i-1]);
        if(p<=i-1) chmin(DP[i],DP2.prod(p,i)-A[i-1]);
        DP1.set(i,DP[i]+Y[i]-X[i]);
        DP2.set(i,DP[i]+Y[i]+X[i]);
    }
    cout<<DP[N]<<endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();
}
0