結果

問題 No.704 ゴミ拾い Medium
ユーザー gyouzasushigyouzasushi
提出日時 2022-05-25 10:12:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 293 ms / 1,500 ms
コード長 4,834 bytes
コンパイル時間 1,977 ms
コンパイル使用メモリ 206,552 KB
実行使用メモリ 17,528 KB
最終ジャッジ日時 2023-10-20 19:07:36
合計ジャッジ時間 10,463 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,152 KB
testcase_01 AC 5 ms
8,152 KB
testcase_02 AC 6 ms
8,152 KB
testcase_03 AC 5 ms
8,156 KB
testcase_04 AC 6 ms
8,156 KB
testcase_05 AC 6 ms
8,156 KB
testcase_06 AC 6 ms
8,156 KB
testcase_07 AC 6 ms
8,156 KB
testcase_08 AC 5 ms
8,156 KB
testcase_09 AC 6 ms
8,156 KB
testcase_10 AC 5 ms
8,156 KB
testcase_11 AC 5 ms
8,156 KB
testcase_12 AC 6 ms
8,156 KB
testcase_13 AC 5 ms
8,156 KB
testcase_14 AC 7 ms
8,184 KB
testcase_15 AC 7 ms
8,184 KB
testcase_16 AC 6 ms
8,184 KB
testcase_17 AC 7 ms
8,184 KB
testcase_18 AC 7 ms
8,184 KB
testcase_19 AC 7 ms
8,184 KB
testcase_20 AC 6 ms
8,184 KB
testcase_21 AC 7 ms
8,184 KB
testcase_22 AC 7 ms
8,184 KB
testcase_23 AC 7 ms
8,184 KB
testcase_24 AC 293 ms
17,528 KB
testcase_25 AC 284 ms
17,528 KB
testcase_26 AC 279 ms
17,528 KB
testcase_27 AC 289 ms
17,528 KB
testcase_28 AC 282 ms
17,528 KB
testcase_29 AC 281 ms
17,528 KB
testcase_30 AC 283 ms
17,528 KB
testcase_31 AC 291 ms
17,528 KB
testcase_32 AC 288 ms
17,528 KB
testcase_33 AC 272 ms
17,528 KB
testcase_34 AC 245 ms
17,528 KB
testcase_35 AC 230 ms
17,528 KB
testcase_36 AC 234 ms
17,528 KB
testcase_37 AC 229 ms
17,528 KB
testcase_38 AC 240 ms
17,528 KB
testcase_39 AC 234 ms
17,528 KB
testcase_40 AC 234 ms
17,528 KB
testcase_41 AC 226 ms
17,528 KB
testcase_42 AC 235 ms
17,528 KB
testcase_43 AC 235 ms
17,528 KB
testcase_44 AC 6 ms
8,152 KB
testcase_45 AC 5 ms
8,152 KB
testcase_46 AC 290 ms
17,528 KB
testcase_47 AC 281 ms
17,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#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())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
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;
}
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;
}
#pragma region SegmentTree
template <class S, S (*op)(S, S), S (*e)()>
struct SegmentTree {
public:
    SegmentTree() : SegmentTree(0) {
    }
    SegmentTree(int n) : SegmentTree(std::vector<S>(n, e())) {
    }
    SegmentTree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::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;
    int ceil_pow2(int n) {
        int x = 0;
        while ((1U << x) < (unsigned int)(n)) x++;
        return x;
    }
    void update(int k) {
        d[k] = op(d[2 * k], d[2 * k + 1]);
    }
};
#pragma endregion
using S = ll;
S op(S a, S b) {
    return min(a, b);
}
S e() {
    return LINF;
}
int main() {
    int n;
    cin >> n;
    vector<ll> a(n), x(n), y(n);
    cin >> a >> x >> y;
    vector<ll> dp(n + 1, LINF);
    dp[0] = 0;
    SegmentTree<S, op, e> large_x(100100), small_x(100100);
    rep(i, n) {
        {
            ll val = large_x.get(x[i]);
            chmin(val, dp[i] + y[i] + x[i]);
            large_x.set(x[i], val);
            chmin(dp[i + 1], large_x.prod(a[i], 100100) - a[i]);
        }
        {
            ll val = small_x.get(x[i]);
            chmin(val, dp[i] + y[i] - x[i]);
            small_x.set(x[i], val);
            chmin(dp[i + 1], small_x.prod(0, a[i]) + a[i]);
        }
    }
    cout << dp[n] << '\n';
}
0