#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
#include <climits>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
typedef long long ll;
ll linf = 1e15+1;
inline void scoobydoobydoo(){
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
if (empty())return -linf;
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
LineContainer LC;
const int MAXN = 2e5+1;
ll mem[MAXN];
ll pref[MAXN];
ll heights[MAXN];
int n;
ll dp(){
mem[0] = 0;
LC.add(2*heights[0], -pow(heights[0],2)+pref[0]);
//cout << mem[0] << " ";
for (int i = 1; i < n; i++){
mem[i] = pref[i-1]+pow(heights[i], 2)-LC.query(heights[i]);
//cout << mem[i] << " ";
LC.add(2*heights[i], -mem[i]-pow(heights[i], 2)+pref[i]);
}
//cout << endl;
return mem[n-1];
}
int main(){
scoobydoobydoo();
cin >> n;
for (int i = 0; i < n; i++) cin >> heights[i];
for (int i = 0; i < n; i++){
cin >> pref[i];
pref[i] += (i == 0 ? 0 : pref[i-1]);
}
cout << dp() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4580 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
5728 KB |
Output is correct |
2 |
Correct |
30 ms |
5720 KB |
Output is correct |
3 |
Correct |
30 ms |
5720 KB |
Output is correct |
4 |
Correct |
29 ms |
5464 KB |
Output is correct |
5 |
Correct |
26 ms |
7000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4580 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
30 ms |
5728 KB |
Output is correct |
7 |
Correct |
30 ms |
5720 KB |
Output is correct |
8 |
Correct |
30 ms |
5720 KB |
Output is correct |
9 |
Correct |
29 ms |
5464 KB |
Output is correct |
10 |
Correct |
26 ms |
7000 KB |
Output is correct |
11 |
Correct |
29 ms |
5720 KB |
Output is correct |
12 |
Correct |
30 ms |
5724 KB |
Output is correct |
13 |
Correct |
25 ms |
5740 KB |
Output is correct |
14 |
Correct |
31 ms |
5724 KB |
Output is correct |
15 |
Correct |
38 ms |
11792 KB |
Output is correct |
16 |
Correct |
26 ms |
6748 KB |
Output is correct |
17 |
Correct |
16 ms |
5724 KB |
Output is correct |
18 |
Correct |
16 ms |
5724 KB |
Output is correct |