【输入样例一】
6
1 2 4 5 6 7
1 2 2 4 2 1
【输入样例二】
4
10 20 49 59
0 10 10 0

【输出样例一】
1.000
【输出样例二】
14.500

代码

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#include <cmath>
#define mcp(a,b) fabs((a)-(b))<eps
using namespace std;
const int MAXN = 300;
const double eps = 1e-8;
const int INF = 1000000000;
struct Point{
double x, y;
Point(){x=y=0;}
bool operator<(const Point& pt) const{
return x < pt.x;
}
bool operator>(Point pt) const{
return x > pt.x;
}
}List[MAXN+10], List2[MAXN+10];
struct Line{
double a, b;
Point GetD(const Line& c){
Point ret;
ret.x = (1.0 * (c.b - b)) / (a - c.a);
ret.y = a * ret.x + b;
return ret;
}
void Make(Point _a, Point _b){
a = (_a.y - _b.y) / (_a.x - _b.x);
b = _a.y - a * _a.x;
}
bool operator == (const Line& c) {
return c.a + eps >= a && c.a - eps <= a;
}
}T[MAXN+10];
stack<Line> ans;
stack<Point> jd;
bool cmp(Line a, Line b){
if(mcp(a.a, b.a))
return a.b < b.b;
return a.a < b.a;
}
bool cmp3(Point a, Point b){
return a.x < b.x;
}
int pcmp(Point p, Line l){
double y = l.a * p.x + l.b;
if(y >= p.y-eps)
return -1;
return 1;
}
void solve(int n){
int tn = 0;
sort(T+1, T+1+n, cmp);
for(int i=1;i<=n;i++){
while(mcp(T[i].a,T[i+1].a)) i++;
T[++tn] = T[i];
}
ans.push(T[1]);
for(int i=2;i<=tn;i++){
while(!jd.empty()){
Point tp = jd.top();
if(pcmp(tp, T[i]) <= 0){
jd.pop();
ans.pop();
}else break;
}
jd.push(T[i].GetD(ans.top()));
ans.push(T[i]);
}
}
int main(){
Line tmp;
int n;
scanf("%d", &n);
for(int i=1;i<=n;i++) scanf("%lf", &List[i].x);
for(int i=1;i<=n;i++) scanf("%lf", &List[i].y);
for(int i=2;i<=n;i++)
T[i-1].Make(List[i-1], List[i]);
solve(n-1);
int cnt = 0;
while(!jd.empty()){
List2[++cnt] = jd.top();
jd.pop();
}
sort(List2+1, List2+1+cnt, cmp3);
double answer = 1e20;
for(int i=1;i<n;i++){
tmp.Make(List[i], List[i+1]);
int pos = lower_bound(List2+1, List2+1+cnt, List[i]) - List2;
for(int j=pos;List2[j].x <= List[i+1].x+eps && j <= cnt;j++)
answer = min(answer, max(0.0 , List2[j].y - List2[j].x*tmp.a - tmp.b));
}
int ct2 = ans.size();
int ctmp = ct2;
while(!ans.empty()){
T[--ct2] = ans.top();
ans.pop();
}
ct2 = ctmp;
int tp = lower_bound(List+1, List+1+n, List2[1]) - List;
if(List[tp].x >= List2[1].x-eps) tp--;
while(tp){
tp--;
}
tp = lower_bound(List+1, List+1+n, List2[cnt]) - List;
while(tp <= n){
tp++;
}
for(int i=1;i<cnt;i++){
tp = lower_bound(List+1, List+1+n, List2[i]) - List;
for(int j=tp;List[j].x <= List2[i+1].x && j <= n;j++)
}