Submission #938342

#TimeUsernameProblemLanguageResultExecution timeMemory
938342WendidIask0303Building Bridges (CEOI17_building)C++17
100 / 100
60 ms9676 KiB
#include <iostream> #include <set> using namespace std; struct Line { mutable long long k, m, p; bool operator<(const Line& o) const { return k < o.k; } bool operator<(long long x) const { return p < x; } }; struct LineContainer : multiset<Line, less<>> { // (for doubles, use inf = 1/.0, div(a,b) = a/b) static const long long inf = 1000000000000000001; long long div(long long a, long long b){ 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(long long k, long long 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)); } } long long query(long long x) { auto l = *lower_bound(x); return l.k * x + l.m; } }; long long h[100001]; long long w[100001]; long long dp[100001]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> h[i]; } for (int i = 1; i <= n; ++i) { cin >> w[i]; w[i] += w[i-1]; } LineContainer hull; hull.add(h[1]*2, -1*h[1]*h[1]+w[1]); for (int i=2; i<=n; ++i) { dp[i] = w[i-1]+h[i]*h[i]-hull.query(h[i]); hull.add(h[i]*2, -1*h[i]*h[i]+w[i]-dp[i]); } cout << dp[n]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...