坑点是凸包的点只有2个的情况。
#include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <cstdio> using namespace std; typedef unsigned long long ll; #define maxn 111111 #define pi acos (-1) #define rotate Rotate const double eps = 1e-10; int dcmp (double x) { if (fabs (x) < eps) return 0; else return x < 0 ? -1 : 1; } struct point { double x, y; point (double _x = 0, double _y = 0) : x(_x), y(_y) {} point operator - (point a) const { return point (x-a.x, y-a.y); } point operator + (point a) const { return point (x+a.x, y+a.y); } bool operator < (const point &a) const { return x < a.x || (x == a.x && y < a.y); } bool operator == (const point &a) const { return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0; } }p[maxn]; double cross (point a, point b) { return a.x*b.y-a.y*b.x; } double dot (point a, point b) { return a.x*b.x + a.y*b.y; } double AngleToRad (double x) { return x*pi/180; } point rotate (point a, double rad) { return point (a.x*cos (rad)-a.y*sin (rad), a.x*sin (rad)+a.y*cos (rad)); } 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.0; } int n, m, tot; double l; point ch[maxn]; int ConvexHull () { 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-1]) <= 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-1]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } double dis (point a, point b) { double xx = a.x-b.x, yy = a.y-b.y; return sqrt (xx*xx + yy*yy); } const double INF = 1e30; int main () { //freopen ("in", "r", stdin); int t, kase = 0; scanf ("%d", &t); while (t--) { printf ("Case #%d: ", ++kase); scanf ("%d", &n); for (int i = 0; i < n; i++) { scanf ("%lf%lf", &p[i].x, &p[i].y); } if (n == 1) { printf ("0.000\n"); continue; } m = ConvexHull (); double sum_x = 0, sum_y = 0, ans = INF; for (int i = 0; i < n; i++) { sum_x += p[i].x, sum_y += p[i].y; } if (m == 2 || m == 1) { printf ("0.000\n"); continue; } for (int i = 0; i < m; i++) { int j = (i+1)%m; double a = ch[j].y-ch[i].y; double b = ch[i].x-ch[j].x; double c = ch[i].y*(ch[j].x-ch[i].x) - ch[i].x*(ch[j].y-ch[i].y); ans = min (ans, fabs (sum_x*a + sum_y*b + n*c) / sqrt (a*a + b*b)); } printf ("%.3f\n", ans/n); } return 0; }