#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(mid)
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 |
36 ms |
62968 KB |
Output is correct |
2 |
Incorrect |
36 ms |
62968 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
93 ms |
65400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
62968 KB |
Output is correct |
2 |
Incorrect |
36 ms |
62968 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |