Submission #856318

#TimeUsernameProblemLanguageResultExecution timeMemory
856318CyanmondBuilding Bridges (CEOI17_building)C++17
100 / 100
57 ms10700 KiB
#include <bits/stdc++.h> using namespace std; #define rep(i, l, r) for (int i = (l); i < (r); ++i) #define per(i, l, r) for (int i = (r - 1); i >= l; --i) #define ALL(x) (x).begin(), (x).end() using i64 = long long; struct CHT { // naive struct Line { i64 a, b; i64 eval(i64 x) { return a * x + b; } }; int n, siz, lg; vector<i64> xs; vector<Line> data; CHT(vector<i64> xs_) { xs = xs_; n = (int)xs.size(); lg = 0; while ((1 << lg) < n) ++lg; siz = 1 << lg; while ((int)xs.size() < siz) xs.push_back(1ll << 30); n = siz; data.assign(2 * siz, {1ll << 30, 1ll << 60}); } void add(int i, int l, int r, Line ln) { const int m = (l + r) / 2; const int lx = xs[l], mx = xs[m]; if (data[i].eval(mx) > ln.eval(mx)) { swap(data[i], ln); } if (r - l == 1) return; if (data[i].eval(lx) > ln.eval(lx)) { add(2 * i, l, m, ln); } else { add(2 * i + 1, m, r, ln); } } void add(i64 x, i64 y) { Line ln = {x, y}; add(1, 0, siz, ln); } i64 calcMin(int i) { i64 x = xs[i]; i += siz; i64 ret = 1ll << 60; while (i != 0) { ret = min(ret, data[i].eval(x)); i /= 2; } return ret; } }; void main_() { int N; cin >> N; vector<i64> H(N), W(N); for (auto &e : H) cin >> e; for (auto &e : W) cin >> e; vector<i64> dp(N, 1ll << 60); dp[0] = 0; auto xs = H; sort(ALL(xs)); CHT cht(xs); cht.add(-2 * H[0], -W[0] + dp[0] + H[0] * H[0]); i64 sumW = W[0]; rep(i, 1, N) { const int p = (int)(lower_bound(ALL(xs), H[i]) - xs.begin()); const auto mn = cht.calcMin(p); dp[i] = mn + H[i] * H[i] + sumW; sumW += W[i]; cht.add(-2 * H[i], -sumW + dp[i] + H[i] * H[i]); } cout << dp[N - 1] << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); main_(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...