#include <bits/stdc++.h>
using namespace std;
#define ll long long int
//c + (hi - x)^2
// c + hi^2 + x^2 - 2hix
//lowest
struct line {
ll m,c,x;
bool t;
};
bool operator<(line a, line b){
if (a.t || b.t) return a.x < b.x;
if (a.m == b.m) return a.c > b.c;
return a.m > b.m;
}
double isect(line a, line b){
if (a.m == b.m) return INT_MIN;
return ((double) (b.c-a.c) / (double) (a.m-b.m));
}
set<line> all;
void add(line a){
auto it = all.lower_bound(a);
if (it != all.end() && it != all.begin()){
auto it2 = it;
it2--;
if (isect(*it2,a) >= isect(a,*it)) return;
}
//remove left
while (it != all.begin()){
auto it2 = it;
it2--;
if (it2 == all.begin()) break;
auto it3 = it2;
it3--;
if (isect(*it3,*it2) >= isect(*it2,a)) all.erase(it2);
else break;
}
//remove right
while (it != all.end()){
auto it2 = it;
it2++;
if (it2 == all.end()) break;
if (isect(a,*it) >= isect(*it,*it2)){
all.erase(it);
it = it2;
} else {
break;
}
}
if (it != all.begin()){
line an = *prev(it);
an.x = isect(*prev(it),a);
all.erase(prev(it));
all.insert(an);
//it = all.lower_bound(a);
}
if (it != all.end()) a.x = isect(a,*it);
all.insert(a);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
ll h[n];
ll w[n];
for (int i =0; i < n; i++) cin >> h[i];
for (int i =0; i < n; i++) cin >> w[i];
ll csum = 0;
for (int i =0; i < n; i++) csum += w[i];
//cout << csum << '\n';
line temp;
temp.m = -2*h[0];
temp.c = csum + h[0]*h[0]-w[0];
temp.x = INT_MAX;
temp.t = false;
all.insert(temp);
// cout << temp.c - h[0]*h[0]<< "\n";
for (int i = 1; i < n; i++){
temp.t = true;
temp.x = h[i];
auto it = all.lower_bound(temp);
temp.c = 2*h[i]*h[i] + ((*it).m * h[i] + (*it).c) - w[i];
if (i == n-1) cout << temp.c - h[i]*h[i] << '\n';
temp.m = -2*h[i];
temp.t = false;
add(temp);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
2012 KB |
Output is correct |
2 |
Correct |
45 ms |
2060 KB |
Output is correct |
3 |
Correct |
47 ms |
2060 KB |
Output is correct |
4 |
Correct |
42 ms |
2024 KB |
Output is correct |
5 |
Correct |
51 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |