Submission #1266983

#TimeUsernameProblemLanguageResultExecution timeMemory
1266983canhnam357Building Bridges (CEOI17_building)C++20
100 / 100
42 ms9796 KiB
#include <bits/stdc++.h> 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 = LLONG_MAX; long long div(long long a, long long 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(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) { assert(!empty()); auto l = *lower_bound(x); return l.k * x + l.m; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<long long> h(n + 1), w(n + 1); for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; } auto pre = w; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + w[i]; } // dp[r] = dp[l] + pre[r - 1] - pre[l] + h[l] * h[l] - 2 * h[l] * h[r] + h[r] * h[r] // -2 * h[l], dp[l] - pre[l] + h[l] * h[l] LineContainer lc; vector<long long> dp(n + 1, LLONG_MAX); dp[1] = 0; lc.add(2 * h[1], -dp[1] + pre[1] - h[1] * h[1]); for (int i = 2; i <= n; i++) { dp[i] = -lc.query(h[i]) + h[i] * h[i] + pre[i - 1]; lc.add(2 * h[i], -dp[i] + pre[i] - h[i] * h[i]); } cout << dp[n]; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...