Submission #1307539

#TimeUsernameProblemLanguageResultExecution timeMemory
1307539chithanhnguyenBuilding Bridges (CEOI17_building)C++20
100 / 100
38 ms9780 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))

#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';

const int MAXN = 1e5 + 5;
const int INF  = 1e18 + 5;
int n, h[MAXN], w[MAXN], dp[MAXN], pref[MAXN];

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> h[i];
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
        pref[i] = pref[i - 1] + w[i];
    }
    fill(dp + 1, dp + n + 1, INF);
    dp[1] = 0;
}

namespace brute{
    void solve() {
        for (int i = 2; i <= n; ++i) {
            for (int j = i - 1; j >= 1; --j) {
                int cur = dp[j] + (h[i] - h[j]) * (h[i] - h[j]) + pref[i - 1] - pref[j];
                dp[i] = min(dp[i], cur);
            }
        }

        cout << dp[n];
    }
}

template<bool isMin> // true for min, false for max
struct LineContainer{
    static const long long INF = LLONG_MAX;
    struct Line{
        long long a, b; // ax + b
        mutable long long p;
        bool operator < (const Line &other) const {
            if (other.a == INF && other.b == INF) return p < other.p;
            return a < other.a;
        }

        long long calc(long long x) const {return a * x + b;}
    };

    multiset<Line> hull;

    long long div(long long a, long long b) {
        return a/b - ((a ^ b) < 0 && a % b);
    }

    bool isect(typename multiset<Line>::iterator x, typename multiset<Line>::iterator y) {
        if (y == hull.end()) {x->p = INF; return 0;}
        if (x->a == y->a) x->p = (x->b > y->b ? INF : -INF);
        else x->p = div(y->b - x->b, x->a - y->a);
        return x->p >= y->p;
    }

    // Add line y = ax + b
    void add(long long a, long long b) {
        if (isMin) {a = -a; b = -b;}

        auto z = hull.insert({a, b, 0}), y = z++, x = y;
		while (isect(y, z)) z = hull.erase(z);
		if (x != hull.begin() && isect(--x, y)) isect(x, y = hull.erase(y));
		while ((y = x) != hull.begin() && (--x)->p >= y->p)
			isect(x, hull.erase(y));
    }

    // Query at x
    long long query(long long x) {
        assert(!hull.empty());
        Line l = *hull.lower_bound({INF, INF, x});
        long long res = l.calc(x);
        return isMin ? -res : res;
    }
};

namespace full{
    void solve() {
        LineContainer<true> cht;
        for (int i = 2; i <= n; ++i) {
            cht.add(h[i - 1], dp[i - 1] + h[i - 1] * h[i - 1] - pref[i - 1]);
            int fixed = h[i] * h[i] + pref[i - 1];
            int getmi = cht.query(-2*h[i]);
            dp[i] = fixed + getmi;
        }

        cout << dp[n];
    }
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    full::solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...