Submission #856318

# Submission time Handle Problem Language Result Execution time Memory
856318 2023-10-03T06:02:26 Z Cyanmond Building Bridges (CEOI17_building) C++17
100 / 100
57 ms 10700 KB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define per(i, l, r) for (int i = (r - 1); i >= l; --i)
#define ALL(x) (x).begin(), (x).end()

using i64 = long long;

struct CHT {
    // naive
    struct Line {
        i64 a, b;
        i64 eval(i64 x) {
            return a * x + b;
        }
    };

    int n, siz, lg;
    vector<i64> xs;
    vector<Line> data;

    CHT(vector<i64> xs_) {
        xs = xs_;
        n = (int)xs.size();
        lg = 0;
        while ((1 << lg) < n) ++lg;
        siz = 1 << lg;
        while ((int)xs.size() < siz) xs.push_back(1ll << 30);
        n = siz;
        data.assign(2 * siz, {1ll << 30, 1ll << 60});
    }

    void add(int i, int l, int r, Line ln) {
        const int m = (l + r) / 2;
        const int lx = xs[l], mx = xs[m];
        if (data[i].eval(mx) > ln.eval(mx)) {
            swap(data[i], ln);
        }
        if (r - l == 1) return;
        if (data[i].eval(lx) > ln.eval(lx)) {
            add(2 * i, l, m, ln);
        } else {
            add(2 * i + 1, m, r, ln);
        }
    }

    void add(i64 x, i64 y) {
        Line ln = {x, y};
        add(1, 0, siz, ln);
    }

    i64 calcMin(int i) {
        i64 x = xs[i];
        i += siz;
        i64 ret = 1ll << 60;
        while (i != 0) {
            ret = min(ret, data[i].eval(x));
            i /= 2;
        }
        return ret;
    }
};

void main_() {
    int N;
    cin >> N;
    vector<i64> H(N), W(N);
    for (auto &e : H) cin >> e;
    for (auto &e : W) cin >> e;

    vector<i64> dp(N, 1ll << 60);
    dp[0] = 0;

    auto xs = H;
    sort(ALL(xs));

    CHT cht(xs);

    cht.add(-2 * H[0], -W[0] + dp[0] + H[0] * H[0]);
    i64 sumW = W[0];
    rep(i, 1, N) {
        const int p = (int)(lower_bound(ALL(xs), H[i]) - xs.begin());
        const auto mn = cht.calcMin(p);
        dp[i] = mn + H[i] * H[i] + sumW;
        sumW += W[i];
        cht.add(-2 * H[i], -sumW + dp[i] + H[i] * H[i]);
    }

    cout << dp[N - 1] << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    main_();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 9288 KB Output is correct
2 Correct 54 ms 10304 KB Output is correct
3 Correct 57 ms 10304 KB Output is correct
4 Correct 52 ms 10408 KB Output is correct
5 Correct 31 ms 10316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 53 ms 9288 KB Output is correct
7 Correct 54 ms 10304 KB Output is correct
8 Correct 57 ms 10304 KB Output is correct
9 Correct 52 ms 10408 KB Output is correct
10 Correct 31 ms 10316 KB Output is correct
11 Correct 52 ms 10700 KB Output is correct
12 Correct 53 ms 10484 KB Output is correct
13 Correct 45 ms 10568 KB Output is correct
14 Correct 56 ms 10648 KB Output is correct
15 Correct 34 ms 10312 KB Output is correct
16 Correct 31 ms 10304 KB Output is correct
17 Correct 24 ms 10560 KB Output is correct
18 Correct 24 ms 10564 KB Output is correct