#include <bits/stdc++.h>
using namespace std;
const int k_N = 1e5 + 3;
const int k_Max = 1e6 + 3;
const long long k_Inf = 1e18;
struct LiChaoTree{
struct Line{
long long a, b;
Line(){}
Line(long long a, long long b): a(a), b(b){}
long long operator()(long long x){
return a * x + b;
}
};
Line lines[4 * k_Max];
LiChaoTree(){
for(int i = 0; i < 4 * k_Max; ++i)
lines[i] = {0, k_Inf};
}
//[l, r)
void update(int idx, int l, int r, Line new_line){
int mid = (l + r) >> 1;
bool lb = new_line(l) < lines[idx](l);
bool mb = new_line(mid) < lines[idx](mid);
if(mb)
swap(lines[idx], new_line);
if(r - l == 1) return;
if(lb != mb)
update(2 * idx + 1, l, mid, new_line);
else
update(2 * idx + 2, mid, r, new_line);
}
long long get(int idx, int l, int r, int x){
if(x < l || r < x) return k_Inf;
if(r - l == 1) return lines[idx](x);
int mid = (l + r) >> 1;
return min(lines[idx](x), min(get(2 * idx + 1, l, mid, x), get(2 * idx + 2, mid, r, x)));
}
};
int n;
long long h[k_N], w[k_N];
long long dp[k_N];
LiChaoTree li_chao;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; ++i)
cin >> h[i];
for(int i = 0; i < n; ++i)
cin >> w[i];
dp[n - 1] = 0;
li_chao.update(0, 0, k_Max, LiChaoTree::Line(-2 * h[n - 1], h[n - 1] * h[n - 1]));
long long sum = 0;
for(int i = n - 2; i >= 0; --i){
dp[i] = li_chao.get(0, 0, k_Max, h[i]) + h[i] * h[i] + sum;
sum += w[i];
li_chao.update(0, 0, k_Max, LiChaoTree::Line(-2 * h[i], h[i] * h[i] + dp[i] - sum));
}
//dp[i] = min(sum + h[i] * h[i] - 2 * h[i] * h[j] + h[j] * h[j] + dp[j], dp[i]);
cout << dp[0] << "\n";
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
62976 KB |
Output is correct |
2 |
Correct |
35 ms |
62976 KB |
Output is correct |
3 |
Correct |
31 ms |
62916 KB |
Output is correct |
4 |
Correct |
31 ms |
62968 KB |
Output is correct |
5 |
Correct |
31 ms |
62968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
65400 KB |
Output is correct |
2 |
Correct |
80 ms |
66296 KB |
Output is correct |
3 |
Correct |
86 ms |
66296 KB |
Output is correct |
4 |
Correct |
78 ms |
66168 KB |
Output is correct |
5 |
Correct |
74 ms |
66296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
62976 KB |
Output is correct |
2 |
Correct |
35 ms |
62976 KB |
Output is correct |
3 |
Correct |
31 ms |
62916 KB |
Output is correct |
4 |
Correct |
31 ms |
62968 KB |
Output is correct |
5 |
Correct |
31 ms |
62968 KB |
Output is correct |
6 |
Correct |
84 ms |
65400 KB |
Output is correct |
7 |
Correct |
80 ms |
66296 KB |
Output is correct |
8 |
Correct |
86 ms |
66296 KB |
Output is correct |
9 |
Correct |
78 ms |
66168 KB |
Output is correct |
10 |
Correct |
74 ms |
66296 KB |
Output is correct |
11 |
Correct |
94 ms |
66552 KB |
Output is correct |
12 |
Correct |
89 ms |
66424 KB |
Output is correct |
13 |
Correct |
84 ms |
66424 KB |
Output is correct |
14 |
Correct |
94 ms |
66424 KB |
Output is correct |
15 |
Correct |
74 ms |
66168 KB |
Output is correct |
16 |
Correct |
102 ms |
66168 KB |
Output is correct |
17 |
Correct |
72 ms |
66424 KB |
Output is correct |
18 |
Correct |
68 ms |
66424 KB |
Output is correct |