Submission #976611

#TimeUsernameProblemLanguageResultExecution timeMemory
9766110x34cBuilding Bridges (CEOI17_building)C++17
100 / 100
39 ms9936 KiB
#include <bits/stdc++.h> #define ll long long #define pii pair<int, int> #define endl '\n' #define int ll using namespace std; const int INF = 1e13; 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<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const ll inf = LLONG_MAX; ll div(ll a, ll b) { // floored division return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) return x->p = inf, 0; if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m) { k *= -1; m *= -1; 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 -1*(l.k * x + l.m); } }; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<int> pillars(N); vector<int> psum(N + 1, 0); for(int i = 0; i < N; i++) { cin >> pillars[i]; } for(int i = 1; i <= N; i++) { cin >> psum[i]; psum[i] += psum[i - 1]; } LineContainer cht; vector<int> dp(N); cht.add(-2*pillars[0], pillars[0]*pillars[0] - psum[1]); for(int i = 1; i < N; i++) { dp[i] = dp[0] + (pillars[i] - pillars[0])*(pillars[i] - pillars[0]) + psum[i] - psum[1]; dp[i] = pillars[i]*pillars[i] + psum[i] + cht.query(pillars[i]); cht.add(-2*pillars[i], pillars[i]*pillars[i] - psum[i + 1] + dp[i]); } cout << dp[N - 1] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...