#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e18;
struct Line {
ll m, b;
};
ll f(Line l, ll x) {
return l.m * x + l.b;
}
struct Node {
Line line;
Node* lchild = nullptr;
Node* rchild = nullptr;
};
ll query(Node* &node, ll x, int l = 0, int r = 1e6+5) {
if (node == nullptr) return INF;
int m = (l+r)/2;
if (x <= m) return min(f(node->line, x), query(node->lchild, x, l, m));
else return min(f(node->line, x), query(node->rchild, x, m+1, r));
}
void insert(Node* &node, Line nw, int l = 0, int r = 1e6+5) {
if (node == nullptr) {
node = new Node();
node->line = nw;
return;
}
int m = (l+r)/2;
if (f(nw, m) < f(node->line, m)) swap(nw, node->line);
if (f(nw, l) < f(node->line, l))
insert(node->lchild, nw, l, m);
if (f(nw, r) < f(node->line, r))
insert(node->rchild,nw,m+1,r);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<ll> h(n+1), w(n+1);
vector<ll> pre(n+1);
for (int i = 1; i <= n; ++i) {
cin>>h[i];
}
for (int i = 1; i <= n; ++i) {
cin>>w[i];
pre[i] = pre[i-1] + w[i];
}
vector<ll>dp(n+1);
dp[1] = 0;
w[1] = INF;
Node* tree = nullptr;
for (int i = 1; i <= n; ++i) {
if (i > 1) dp[i] = query(tree, h[i]) + h[i] * h[i] + pre[i-1];
Line ln;
ln.m = -2*h[i];
ln.b = dp[i] + h[i] * h[i] - pre[i];
insert(tree, ln);
//cout << dp[i] << " ";
}
cout << dp[n]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4704 KB |
Output is correct |
2 |
Correct |
41 ms |
4696 KB |
Output is correct |
3 |
Correct |
38 ms |
4732 KB |
Output is correct |
4 |
Correct |
37 ms |
4480 KB |
Output is correct |
5 |
Correct |
28 ms |
5952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
41 ms |
4704 KB |
Output is correct |
7 |
Correct |
41 ms |
4696 KB |
Output is correct |
8 |
Correct |
38 ms |
4732 KB |
Output is correct |
9 |
Correct |
37 ms |
4480 KB |
Output is correct |
10 |
Correct |
28 ms |
5952 KB |
Output is correct |
11 |
Correct |
37 ms |
4844 KB |
Output is correct |
12 |
Correct |
37 ms |
4716 KB |
Output is correct |
13 |
Correct |
30 ms |
4580 KB |
Output is correct |
14 |
Correct |
52 ms |
4836 KB |
Output is correct |
15 |
Correct |
32 ms |
8908 KB |
Output is correct |
16 |
Correct |
28 ms |
5992 KB |
Output is correct |
17 |
Correct |
16 ms |
4436 KB |
Output is correct |
18 |
Correct |
24 ms |
4416 KB |
Output is correct |