Submission #1162403

#TimeUsernameProblemLanguageResultExecution timeMemory
1162403blackslexBuilding Bridges (CEOI17_building)C++20
100 / 100
42 ms9800 KiB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int n;
bool qrm;

struct line {
    ll m, c;
    mutable ll p;
    line(ll p) : p(p) {}
    line(ll m, ll c) : m(m), c(c) {}
    friend bool operator < (const line &l1, const line &l2) {
        return (qrm ? l1.p < l2.p : l1.m > l2.m);
    }
};

struct convex:multiset<line> {
    ll div (ll a, ll b) {
        return (a / b) - ((a ^ b) < 0 && a % b);
    }
    bool ck (iterator x, iterator y) {
        if (y == end()) return x->p = 1e18, 0;
        if (x->m == y->m) x->p = x->c <= y->c ? 1e18 : -1e18;
        else x->p = div(x->c - y->c, y->m - x->m);
        return x->p >= y->p;
    }
    void add (ll m, ll c) {
        auto y = insert(line(m, c)), x = y++;
        while (ck(x, y)) y = erase(y);
        if ((y = x) != begin() && ck(--x, y)) ck(x, erase(y));
        while ((y = x) != begin() && (--x)->p >= y->p) ck(x, erase(y));
    }
    ll qr (ll x) {
        if (empty()) return 1e18;
        qrm = 1; auto idx = lower_bound(x); qrm = 0;
        return idx->m * x + idx->c;
    }
} hull;

int main() {
    scanf("%d", &n);
    vector<ll> h(n + 5), w(n + 5), pref(n + 5), dp(n + 5);
    for (int i = 1; i <= n; i++) scanf("%lld", &h[i]);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &w[i]);
        pref[i] = pref[i - 1] + w[i];
    }
    dp[1] = 0;
    hull.add(-2 * h[1], dp[1] + h[1] * h[1] - pref[1]);
    for (int i = 2; i <= n; i++) {
        dp[i] = hull.qr(h[i]) + h[i] * h[i] + pref[i - 1];
        hull.add(-2 * h[i], dp[i] + h[i] * h[i] - pref[i]);
    }
    printf("%lld", dp[n]);
}

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   43 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
building.cpp:45:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |     for (int i = 1; i <= n; i++) scanf("%lld", &h[i]);
      |                                  ~~~~~^~~~~~~~~~~~~~~
building.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         scanf("%lld", &w[i]);
      |         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...