#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MASK(x) (1 << x)
const int maxN = 1e5 + 7, maxVal = (int)1e6 + 7, maxLog = 31 - __builtin_clz(maxVal) + 2, INF = (int)1e9 + 7;
struct Line {
ll a, b;
Line() {
a = -INF;
b = INF;
}
Line(ll _a, ll _b) {
a = _a;
b = _b;
}
ll operator() (ll x) {
return a * x + b;
}
} node[MASK(maxLog)];
void ins(int id, int l, int r, Line L) {
if(l > r) return;
if(l == r) {
node[id] = L;
return;
}
int m = (l + r) >> 1;
if(node[id].a > L.a) swap(node[id], L);
if(node[id](m) > L(m)) {
swap(node[id], L);
ins(id << 1 | 1, m + 1, r, L);
}else ins(id << 1, l, m, L);
}
void ins(Line L) {
ins(1, 0, maxVal - 5, L);
}
ll get(int id, int l, int r, int x) {
if(l > r) return INF;
if(l == r) return node[id](x);
int m = (l + r) >> 1;
if(x <= m) return min(node[id](x), get(id << 1, l, m, x));
return min(node[id](x), get(id << 1 | 1, m + 1, r, x));
}
ll get(int x) {
return get(1, 0, maxVal - 5, x);
}
int n, h[maxN], w[maxN];
long long psW[maxN], f1[1007], f[maxN];
long long sqr(long long x) {
return x * x;
}
void subtask1() {
for (int i = 1; i <= n; i++) {
psW[i] = psW[i - 1] + w[i];
f1[i] = sqr(h[i] - h[1]) + psW[i - 1] - psW[1];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
f1[i] = min(f1[i], f1[j] + sqr(h[i] - h[j]) + psW[i - 1] - psW[j]);
}
}
cout << f1[n] << '\n';
}
void subtask2() {
//memset(f, 0x3f, sizeof f);
f[1] = 0;
for (int i = 1; i <= n; i++) {
psW[i] = psW[i - 1] + w[i];
}
ins(Line(-2 * h[1], 0 - psW[1] + sqr(h[1]))); // ax + b
for(int i = 2; i <= n; i++) {
f[i] = get(h[i]) + sqr(h[i]) + psW[i - 1];
ins(Line(-2 * h[i], f[i] - psW[i] + sqr(h[i])));
}
cout << f[n] << endl;
}
int main() {
cin.tie(NULL)->ios_base::sync_with_stdio(NULL);
//freopen("BRIDGES.inp", "r", stdin);
//freopen("BRIDGES.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
for (int i = 1; i <= n; i++) {
cin >> w[i];
}
//if (n <= 1000) subtask1();
subtask2();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
35416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
36188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
35416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |