这篇日志急忙中写完 由于在写公式中写错了一个地方, 后来重写一遍的时候 发现了这个问题 才纠正过来。
不过也够浪费时间的了。
题目很简单。 因为要使所有的点都在一个线的一边。 那么自然回想到 凸包的外层的每一个边 都可以满足题意。
那么下一步就是暴力 每一条边 求其余所有的点 到这条直线的距离就好了。
点到直线的公式 应该都知道。 fabs(ax + by +c)/ sqrt(a*a + b*b);
所有的点到直线的距离加起来。 那么 用公式看的话 就是fabs(a*(x1 + x2 + x3 ....) + b *(y1 + y2 + y3+ .....) + c *(n-2))/ sqrt(a*a + b*b);
然后预处理一下 sum x 和 sum y 就好了。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 10010
#define INF 1<<30
struct Point{
double x,y;
Point(double x = 0, double y = 0):x(x),y(y) {}
};
typedef Point Vector ;
Vector operator + (Vector A, Vector B){ return Vector(A.x + B.x, A.y + B.y);}
Vector operator - (Vector A, Point B){return Vector(A.x - B.x, A.y - B.y);}
Vector operator * (Vector A, double p){return Vector(A.x * p, A.y * p);}
Vector operator / (Vector A, double p){return Vector(A.x / p, A.y / p);}
bool operator < (const Point & a, const Point & b){
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x){
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (const Point & a, const Point & b){
return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B){ return A.x * B.x + A.y * B.y;} // 点积
double Length(Vector A) { return sqrt(Dot(A, A));} //向量长度
double Angle(Vector A, Vector B){ return acos(Dot(A, B)/Length(A)/Length(B));}
//夹角
double Cross(Vector A, Vector B){return A.x*B.y - A.y * B.x;} //叉积(面积两倍)
double Area2(Point A, Point B, Point C){return Cross(B-A,C-A);} // 面积两倍
Vector Rotate(Vector A, double rad){return Vector(A.x*cos(rad) - A.y*sin(rad), A.x*sin(rad)+A.y*cos(rad));}// 旋转后的直线</span>
Vector Normal(Vector A){
double L = Length(A);
return Vector(-A.y/L, A.x/L);
}
Point GetLineIntersection(Point P, Point v, Point Q, Point w){ //求直线 pv 与qw的交点
Vector u = P-Q;
double t = Cross(w, u) / Cross(v, w);
return P+v*t;
}
double DistanceToline(Point P, Point A, Point B){ //点到直线的距离
Vector v1 = B - A,v2 = P - A;
return fabs(Cross(v1,v2))/Length(v1);
}
double DistanceTpsegment(Point P, Point A, Point B){ //点到线段的距离
if(A == B) return Length(P-A);
Vector v1 = B - A, v2 = P - A, v3 = P - B;
if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
else if(dcmp(Dot(v1,v3)) > 0) return Length(v3);
else return fabs(Cross(v1, v2)) / Length(v1);
}
Point GetLinePrijection(Point P, Point A, Point B){ // 点在直线上的投影
Vector v = B-A;
return A+v*(Dot(v, P-A)/Dot(v,v));
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){ // 线段相交判定(不包括在端点处的情况)
double c1 = Cross(a2 - a1,b1-a1) ,c2 = Cross(a2 - a1,b2-a1),
c3 = Cross(b2 - b1, a1-b1), c4 = Cross(b2 - b1, a2 - b1);
return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
bool OnSegment(Point p, Point a1, Point a2){ //线段在端点处是否可能相交
return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}
double ConvexPolygonArea(Point * p,int n){ // 多边形的有向面积
double area = 0;
for(int i = 1; i < n-1; i++)
area += Cross(p[i] - p[0],p[i+1] - p[0]);
return area/2;
}
int ConvexHull(Point *p, int n, Point * ch){ // 求凸包。 点保存在 ch中
sort(p, p+n);
int m = 0;
for(int i = 0; i < n; i++){
while(m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
int k = m;
for(int i = n-2; i >= 0; i--){
while(m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
ch[m++] = p[i];
}
if(n > 1) m --;
return m;
}
int line_get(double & a, double & b, double & c, Point m, Point n){
if(m.x == n.x){
a = 1;
b = 0;
c = -m.x;
}
else{
a = (m.y - n.y)/(m.x - n.x);
b = -1;
c = m.y - (m.y - n.y)/(m.x - n.x)*(m.x);
}
}
int main (){
int num;
int kase = 0;
scanf("%d",&num);
while(num--){
int n;
scanf("%d",&n);
Point p[maxn];
double sumx = 0, sumy = 0;
for(int i = 0; i < n; i++){
scanf("%lf%lf",&p[i].x, &p[i].y);
sumx += p[i].x;
sumy += p[i].y;
}
if(n <= 2){
printf("Case #%d: 0.000\n",++kase);
continue;
}
Point af[maxn];
int m = ConvexHull(p, n, af);
double ans;
double x,y;
double a,b,c;
x = sumx - af[0].x - af[m-1].x;
y = sumy - af[0].y - af[m-1].y;
line_get(a,b,c,af[0],af[m-1]);
ans = fabs(a*x + b*y + c*(n-2))/sqrt(a*a + b*b);
for(int i = 1; i < m; i++){
x = sumx - af[i].x - af[i-1].x;
y = sumy - af[i].y - af[i-1].y;
line_get(a,b,c,af[i],af[i-1]);
ans = min(ans, fabs(a*x + b*y + c*(n-2))/sqrt(a*a + b*b));
}
printf("Case #%d: %.3lf\n",++kase,ans/n);
}
return 0;
}