Submission #819460

#TimeUsernameProblemLanguageResultExecution timeMemory
819460borisAngelovBuilding Bridges (CEOI17_building)C++17
100 / 100
76 ms67176 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; const int max_size = 1000000; const long long inf = (1LL << 62); int n; long long h[maxn]; long long w[maxn]; long long pref[maxn]; long long dp[maxn]; struct line { long long a; long long b; line() { a = 0; b = inf; } line(long long _a, long long _b) { a = _a; b = _b; } long long calc(long long x) { return a * x + b; } }; struct li_chao_tree { line tree[4 * max_size + 20]; void update(int node, int l, int r, line new_line) { if (tree[node].calc(l) < new_line.calc(l) && tree[node].calc(r) < new_line.calc(r)) { return; } int mid = (l + r) / 2; if (tree[node].calc(mid) > new_line.calc(mid)) { swap(tree[node], new_line); } if (tree[node].calc(l) > new_line.calc(l)) { update(2 * node, l, mid, new_line); } if (tree[node].calc(r) > new_line.calc(r)) { update(2 * node + 1, mid + 1, r, new_line); } } long long query(int node, int l, int r, int pos) { if (l == r) { return tree[node].calc(pos); } int mid = (l + r) / 2; if (pos <= mid) { return min(tree[node].calc(pos), query(2 * node, l, mid, pos)); } return min(tree[node].calc(pos), query(2 * node + 1, mid + 1, r, pos)); } void update(const line& new_line) { update(1, 0, max_size, new_line); } long long query(int pos) { return query(1, 0, max_size, pos); } }; li_chao_tree tree; void fastIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { fastIO(); cin >> n; for (int i = 1; i <= n; ++i) { cin >> h[i]; } for (int i = 1; i <= n; ++i) { cin >> w[i]; pref[i] = pref[i - 1] + w[i]; } dp[1] = 0; line curr = line(-(2 * h[1]), h[1] * h[1] - pref[1]); tree.update(curr); for (int i = 2; i <= n; ++i) { dp[i] = h[i] * h[i] + pref[i - 1] + tree.query(h[i]); curr = line(-(2 * h[i]), dp[i] + h[i] * h[i] - pref[i]); tree.update(curr); } cout << dp[n] << endl; return 0; } /* 6 3 8 7 1 6 6 0 -1 9 1 2 0 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...