이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(x, v) for (auto &x : v)
using ll = long long;
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<>> {
const ll inf = 1e18;
ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
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) {
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) {
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
vector<ll> f(n), h(n), w(n);
LineContainer q;
trav(x, h) cin >> x;
trav(x, w) cin >> x;
rep(i, 1, n) w[i] += w[i - 1];
q.add(2 * h[0], - f[0] - h[0] * h[0] + w[0]);
rep(i, 1, n) {
f[i] = h[i] * h[i] + w[i - 1] - q.query(h[i]);
q.add(2 * h[i], - f[i] - h[i] * h[i] + w[i]);
}
cout << f[n - 1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |