Topic and simulated annealing: see http://blog.csdn.net/popoqqq/article/details/39340759
This time with half a plane to write it again ... After the semi-planar intersection is obtained. Enumerate each point of the original and half-plane intersection, and find out the answer can be
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define M 310#define eps 1e-7using namespace std;struct point{double x, y;} Points[m];struct line{point *p1,*p2;double k,b;void get_parameters () {k= (double) (p1->y-p2->y)/(p1->x-p2- >X); b=p1->y-k*p1->x;} BOOL operator < (const line &x) const{if (Fabs (K-X.K) <eps) return B < X.b;return K < X.K;}} Lines[m];int n;double ans=1e11;line *stack[m];int top;inline point get_intersection (const line &l1,const line & L2) {point re;re.x=-(l1.b-l2.b)/(L1.K-L2.K); Re.y=l1.k*re.x+l1.b;return re;} void Insert (line &l) {while (top>=2) {if (Get_intersection (*stack[top],l). x < Get_intersection (*stack[top-1] , *stack[top]). x)--top;elsebreak;} Stack[++top]=&l;} Double F (double x) {int i;double re=0;for (i=1;i<=top;i++) Re=max (re,stack[i]->k*x+stack[i]->b); return re;} Double G (double x) {int i;for (i=n;i;i--) if (x>=points[i].x) Break;return points[i].y+ (x-points[i].x)/(points[i+1].x-points[i].x) * (POINTS[I+1].Y-POINTS[I].Y);} int main () {int i;cin>>n;for (i=1;i<=n;i++) scanf ("%lf", &points[i].x), for (i=1;i<=n;i++) scanf ("%lf", &POINTS[I].Y); for (i=1;i<n;i++) lines[i].p1=points+i,lines[i].p2=points+i+1,lines[i]. Get_parameters (); sort (lines+1,lines+n); for (i=1;i<n;i++) if (i==n-1| | Fabs (LINES[I].K-LINES[I+1].K) >eps) Insert (Lines[i]); for (i=1;i<=n;i++) ans=min (ans,f (points[i].x)-points[i] . y); for (i=1;i<top;i++) {point p=get_intersection (*stack[i],*stack[i+1]); Ans=min (Ans,p.y-g (p.x));} printf ("%.3lf\n", ans);}
Bzoj 1038 ZJOI2008 Lookout tower half-plane intersection