Submission #1134649

#TimeUsernameProblemLanguageResultExecution timeMemory
1134649tuongllBuilding Bridges (CEOI17_building)C++20
100 / 100
36 ms9800 KiB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <ctime>
#include <cassert>
#include <set>
#include <stack>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <bitset>
#include <array>
using ll = long long;
#define debug(x) cout << #x << " = " << x << '\n'
#define separator "===============================================\n"
#define all(a) a.begin(), a.end()
#define SZ(a) (int)(a).size()
using namespace std;
const int mxn = 1e5 + 3;
const ll  mod = 1e9 + 7;
const int inf32 = 2e9;
const ll  inf64 = 3e18;
/*
dp[i] = min cost to reach i-th pillar
dp[i] = min_{j < i} dp[j] + (h[i] - h[j])^2 + (w[j + 1] + ... + w[i - 1])
= dp[j] + h[i]^2 + h[j]^2 - 2*h[i]*h[j] + pref[i - 1] - pref[j]
= (h[i]^2 + pref[i - 1]) - h[j]*2*h[i] + dp[j] + h[j]^2 - pref[j]
given n lines ax + b, given x, find line that minimizes ax+b
*/
struct Line {
    mutable ll k, m, p;
    bool operator<(const Line& o) const { return k < o.k; }
    bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
    ll div(ll a, ll b){
        return a / b - ((a ^ b) < 0 && a % b); }
    bool isect(iterator x, iterator y) {
        if (y == end()) return x->p = inf64, 0;
        if (x->k == y->k) x->p = x->m > y->m ? inf64 : -inf64;
        else x->p = div(y->m - x->m, x->k - y->k);
        return x->p >= y->p;
    }
    void add(ll k, ll m){
        k = -k, m = -m;
        auto z = insert({k, m, 0}), y = z++, x = y;
        while (isect(y, z)) z = erase(z);
        if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p)
            isect(x, erase(y));
    }
    ll query(ll x) {
        assert(!empty());
        auto l = *lower_bound(x);
        return -(l.k * x + l.m);
    }
};
void solve(){
    int n; cin >> n;
    vector<ll> h(n + 1), w(n + 1), pref(n + 1);
    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];
    vector<ll> dp(n + 1);
    LineContainer cht;
    cht.add(h[1], h[1] * h[1] - w[1]);
    for (int i = 2; i <= n; ++i){
        dp[i] = h[i] * h[i] + pref[i - 1] + cht.query(h[i] * -2);
        cht.add(h[i], dp[i] + h[i] * h[i] - pref[i]);
    }
    cout << dp[n];
}
int main(){
	auto start = chrono::steady_clock::now();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    // cin >> t;
    while(t--) solve();
    chrono::duration<double> elapsed {chrono::steady_clock::now() - start};
    cerr << "\n>> Runtime: " << elapsed.count() << "s\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...