This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
const int N = 1e5 + 5;
const int M = 1e6 + 5;
const int inf = 1e9 + 7;
struct node{
int m, b;
double operator * (node x){
return (b - x.b) * 1. / (x.m - m);
}
int get(int x){
return m * x + b;
}
};
struct LiChao{
node tree[M << 2];
LiChao(){
for(int i=0;i<(M << 2);i++){
tree[i] = {inf, inf * inf};
}
}
void add(node v, int lx = 0, int rx = M, int x = 1){
if(lx == rx){
if(v.get(lx) > tree[x].get(lx)) tree[x] = v;
return;
}
if(v.m == tree[x].m){
tree[x].b = min(tree[x].b, v.b);
return;
}
double is = tree[x] * v;
int m = (lx + rx) >> 1;
if(is < m + 1){
if(tree[x].get(m + 1) > v.get(m + 1)) swap(tree[x], v);
add(v, lx, m, x << 1);
} else {
if(tree[x].get(m) > v.get(m)) swap(tree[x], v);
add(v, m + 1, rx, x << 1 | 1);
}
}
int get(int i, int lx = 0, int rx = M, int x = 1){
if(lx == rx){
return tree[x].get(i);
}
int m = (lx + rx) >> 1;
if(i <= m) return min(get(i, lx, m, x << 1), tree[x].get(i));
else return min(get(i, m + 1, rx, x << 1 | 1), tree[x].get(i));
}
}tree;
int h[N], w[N], dp[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
}
for(int i=1;i<=n;i++){
cin>>w[i];
w[i] += w[i-1];
}
tree.add({-2 * h[1], h[1] * h[1] - w[1]});
for(int i=2;i<=n;i++){
dp[i] = tree.get(h[i]) + h[i] * h[i] + w[i-1];
tree.add({-2 * h[i], dp[i] + h[i] * h[i] - w[i]});
}
cout<<dp[n]<<"\n";
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |