uva 11168

凸包+一点直线的知识;

  1 #include <cstdio>
  2 #include <cmath>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #define eps 1e-9
  7 using namespace std;
  8 const double pi = acos(-1);
  9 
 10 int dcmp(double x)
 11 {
 12     return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1);
 13 }
 14 
 15 struct Point
 16 {
 17     double x;
 18     double y;
 19 
 20     Point(double x = 0, double y = 0):x(x), y(y) {}
 21 
 22     bool operator < (const Point& e) const
 23     {
 24         return dcmp(x - e.x) < 0 || (dcmp(x - e.x) == 0 && dcmp(y - e.y) < 0);
 25     }
 26 
 27     bool operator == (const Point& e) const
 28     {
 29         return dcmp(x - e.x) == 0 && dcmp(y - e.y) == 0;
 30     }
 31 };
 32 
 33 typedef Point Vector;
 34 
 35 Vector operator + (Point A, Point B)
 36 {
 37     return Vector(A.x + B.x, A.y + B.y);
 38 }
 39 
 40 Vector operator - (Point A, Point B)
 41 {
 42     return Vector(A.x - B.x, A.y - B.y);
 43 }
 44 
 45 Vector operator * (Point A, double p)
 46 {
 47     return Vector(A.x * p, A.y * p);
 48 }
 49 
 50 Vector operator / (Point A, double p)
 51 {
 52     return Vector(A.x / p, A.y / p);
 53 }
 54 
 55 double cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
 56 Point rotate(Point a,double ang){return Point(a.x*cos(ang)-a.y*sin(ang),a.x*sin(ang)+a.y*cos(ang));}
 57 int convexhull(Point *p,int n,Point *ch)
 58 {
 59     sort(p,p+n);
 60     int m=0;
 61     for(int i=0;i<n;i++)
 62     {
 63         while(m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
 64         ch[m++]=p[i];
 65     }
 66     int k=m;
 67     for(int i=n-2;i>=0;i--)
 68     {
 69         while(m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;
 70         ch[m++]=p[i];
 71     }
 72     if(n>1)m--;
 73     return m;
 74 }
 75 Point p[100009],ch[100009];
 76 
 77 int main()
 78 {
 79     int t,n,ca=1;
 80     scanf("%d",&t);
 81     while(t--)
 82     {
 83         double sumx=0;
 84         double sumy=0;
 85         scanf("%d",&n);
 86         for(int i=0;i<n;i++)
 87         {
 88             scanf("%lf%lf",&p[i].x,&p[i].y);
 89             sumx+=p[i].x;
 90             sumy+=p[i].y;
 91         }
 92         printf("Case #%d: ",ca++);
 93         double mi=99999999999.0;
 94         int m=convexhull(p,n,ch);
 95         for(int i=0;i<m;i++)
 96         {
 97             double a=-ch[(i+1)%m].y+ch[i].y;
 98             double b=ch[(i+1)%m].x-ch[i].x;
 99             double c=ch[i].x*ch[(i+1)%m].y-ch[i].y*ch[(i+1)%m].x;
100             double d=fabs(a*sumx+b*sumy+c*n)/sqrt(a*a+b*b);
101             mi=min(mi,d);
102         }
103         printf("%.3lf
",n>2?mi/n:0);
104     }
105     return 0;
106 }
View Code
原文地址:https://www.cnblogs.com/yours1103/p/3406853.html