#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e6 + 5;
const double eps = 1e-9;
struct Line {
ll m, b;
ll operator()(ll x) { return m * x + b; }
};
struct LiChao {
int n;
vector<Line> tree;
LiChao(int _n) {
n = _n;
tree.resize(4*_n+5);
for(Line &l : tree) l = { (ll)1e9, (ll)1e9 };
}
void insert(int u, int tl, int tr, Line seg) {
if(tl + 1 == tr) {
if(seg(tl) < tree[u](tl)) tree[u] = seg;
return ;
}
int tm = (tl + tr) / 2;
if(tree[u].m < seg.m) swap(tree[u], seg);
if(tree[u](tm) > seg(tm)) {
swap(tree[u], seg);
insert(2*u+1, tl, tm, seg);
} else insert(2*u+2, tm, tr, seg);
}
ll query(int u, int tl, int tr, ll p) {
if(tl + 1 == tr) return tree[u](p);
int tm = (tl + tr) / 2;
if(p < tm) return min(tree[u](p), query(2*u+1, tl, tm, p));
return min(tree[u](p), query(2*u+1, tm, tr, p));
}
void insert(Line seg) { insert(1, 0, n, seg); }
ll query(ll p) { return query(1, 0, n, p); }
};
int32_t main() {
int n;
cin >> n;
vector<ll> h(n+1), w(n+1), pref(n+1);
for(int i=1; i<=n; i++) cin >> h[i];
for(int i=1; i<=n; i++) cin >> w[i];
for(int i=1; i<=n; i++) pref[i] = pref[i-1] + w[i];
vector<ll> dp(n+1, 1e18);
LiChao cht(maxn+5);
dp[1] = 0;
cht.insert({ -2 * h[1], h[1] * h[1] + dp[1] - pref[1] });
//(h[i] - h[j]) * (h[i] - h[j]) = h[i]^2 - 2 * h[i] * h[j] + h[j]
// dp[i] = min(dp[i], dp[j] + h[i] * h[i] - 2 * h[i] * h[j] + h[j] * h[j] + pref[i] - pref[j] - w[i]);
// dp[i] = min(dp[i], (h[i] * h[i] + pref[i] - w[i]) - 2 * h[i] * h[j] + h[j] * h[j] + dp[j] - pref[j] );
for(int i=2; i<=n; i++) {
// for(int j=1; j<i; j++) {
// dp[i] = min(dp[i], (h[i] * h[i] + pref[i] - w[i]) - 2 * h[i] * h[j] + h[j] * h[j] + dp[j] - pref[j] );
// }
dp[i] = cht.query(h[i]) + h[i] * h[i] + pref[i] - w[i];
cht.insert({ -2 * h[i], h[i] * h[i] + dp[i] - pref[i] });
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
62800 KB |
Output is correct |
2 |
Incorrect |
45 ms |
62804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
92 ms |
67152 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
62800 KB |
Output is correct |
2 |
Incorrect |
45 ms |
62804 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |