#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct T {
ll a;
ll b;
};
const int N = (int) 1e5 + 7;
const ll INF = (ll) 1e18 + 7;
int n;
ll h[N];
ll sum[N];
ll dp[N];
T coef[N];
/**
T first
T second
T third
T ff, ss, tt
ff.a * x + ff.b = ss.a * x + ss.b
ff.b - ss.b = x * (ss.a - ff.a)
(ff.b - ss.b) / (ss.a - ff.a) < (ss.b - tt.b) / (tt.a - ss.a)
(ff.b - ss.b) * (tt.a - ss.a) < (ss.b - tt.b) * (ss.a - ff.a)
**/
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
h[i] = x;
}
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
sum[i] = sum[i - 1] + x;
}
dp[1] = 0;
for (int i = 2; i <= n; i++) {
dp[i] = INF;
}
vector<T> all;
for (int r = 2; r <= n; r++) {
T p3 = {- 2 * h[r - 1], dp[r - 1] + h[r - 1] * h[r - 1] - sum[r - 1]};
while ((int) all.size() >= 2) {
T p1 = all[(int) all.size() - 2];
T p2 = all[(int) all.size() - 1];
if ((p3.b - p1.b) * (ll) (p1.a - p2.a) > (p1.a - p3.a) * (ll) (p2.b - p1.b)) {
all.pop_back();
} else {
break;
}
}
all.push_back(p3);
ll x = h[r];
for (auto &ln : all) {
dp[r] = min(dp[r], ln.a * x + ln.b);
}
dp[r] += h[r] * h[r] + sum[r - 1];
}
cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3066 ms |
3824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |