hdu 3007 Buried memory 最远点对

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007

Each person had do something foolish along with his or her growth.But,when he or she did this that time,they could not predict that this thing is a mistake and they will want this thing would rather not happened.
The world king Sconbin is not the exception.One day,Sconbin was sleeping,then swakened by one nightmare.It turned out that his love letters to Dufein were made public in his dream.These foolish letters might ruin his throne.Sconbin decided to destroy the letters by the military exercises's opportunity.The missile is the best weapon.Considered the execution of the missile,Sconbin chose to use one missile with the minimum destruction.
Sconbin had writen N letters to Dufein, she buried these letters on different places.Sconbin got the places by difficult,he wants to know where is the best place launch the missile,and the smallest radius of the burst area. Let's help Sconbin to get the award.
题意描述:给出n封信的二维坐标,找到一个坐标点,使其距离n封信的最远距离最近。
算法分析:最远点对的模板题。需要注意一点:题目中的坐标点没规律,最远点对的算法针对凸边形,所以首先我们得对题目点集凸包化。
  1 #include<iostream>

  2 #include<cstdio>

  3 #include<cstring>

  4 #include<cstdlib>

  5 #include<cmath>

  6 #include<algorithm>

  7 #include<vector>

  8 #define inf 0x7fffffff

  9 #define exp 1e-10

 10 #define PI 3.141592654

 11 using namespace std;

 12 const int maxn=500+10;

 13 

 14 int n;

 15 struct Point

 16 {

 17     double x,y;

 18     Point(double x=0,double y=0):x(x),y(y){}

 19 }an[maxn],bn[maxn];

 20 typedef Point Vector;

 21 double dcmp(double x)

 22 {

 23     if (fabs(x)<exp) return 0;

 24     return x<0 ? -1 : 1;

 25 }

 26 Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }

 27 Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); }

 28 Vector operator * (Vector A,double p) {return Vector(A.x*p , A.y*p); }

 29 Vector operator / (Vector A,double p) {return Vector(A.x/p , A.y/p); }

 30 bool operator < (Point a,Point b) {return a.x<b.x || (a.x==b.x && a.y<b.y); }

 31 bool operator == (Point a,Point b) {return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0; }

 32 

 33 double Dot(Vector A,Vector B) {return A.x*B.x + A.y*B.y ; }

 34 double Length(Vector A,Vector B) {return sqrt(Dot(A,A)); }

 35 double cross(Vector A,Vector B) {return A.x*B.y - B.x*A.y; }

 36 

 37 double dist(Point A,Point B)

 38 {

 39     double xx=(A.x-B.x)*(A.x-B.x);

 40     double yy=(A.y-B.y)*(A.y-B.y);

 41     return sqrt(xx+yy);

 42 }

 43 

 44 Point cur;

 45 double rotating_calipers(Point *ch,int n)

 46 {

 47     int q=1;

 48     double ans=-1;

 49     cur.x=cur.y=0;

 50     ch[n]=ch[0];

 51     for (int p=0 ;p<n ;p++)

 52     {

 53         while (cross(ch[q+1]-ch[p+1],ch[p]-ch[p+1])>cross(ch[q]-ch[p+1],ch[p]-ch[p+1]))

 54             q=(q+1)%n;

 55         double len=dist(ch[p],ch[q]);

 56         double len2=dist(ch[p+1],ch[q+1]);

 57         if (len>ans+exp)

 58         {

 59             ans=len;

 60             cur.x=(ch[p].x+ch[q].x)/2.0;

 61             cur.y=(ch[p].y+ch[q].y)/2.0;

 62         }

 63         if (len2>ans+exp)

 64         {

 65             ans=len2;

 66             cur.x=(ch[p+1].x+ch[q+1].x)/2.0;

 67             cur.y=(ch[p+1].y+ch[q+1].y)/2.0;

 68         }

 69     }

 70     return ans;

 71 }

 72 

 73 int Convexhull(Point *p,int n,Point *ch)

 74 {

 75     sort(p,p+n);

 76     int m=0;

 77     for (int i=0 ;i<n ;i++)

 78     {

 79         while (m>1 && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<0) m--;

 80         ch[m++]=p[i];

 81     }

 82     int k=m;

 83     for (int i=n-2 ;i>=0 ;i--)

 84     {

 85         while (m>k && dcmp(cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]))<0) m--;

 86         ch[m++]=p[i];

 87     }

 88     if (n>1) m--;

 89     return m;

 90 }

 91 

 92 int main()

 93 {

 94     while (scanf("%d",&n)!=EOF && n)

 95     {

 96         for (int i=0 ;i<n ;i++) scanf("%lf%lf",&an[i].x,&an[i].y);

 97         if (n==1) {printf("%.2lf %.2lf 0.00\n",an[0].x,an[0].y);continue; }

 98         if (n==2) {printf("%.2lf %.2lf %.2lf\n",(an[0].x+an[1].x)/2.0,

 99             (an[0].y+an[1].y)/2.0,dist(an[0],an[1])/2.0);continue; }

100         int m=Convexhull(an,n,bn);

101         double ans=rotating_calipers(bn,m);

102         printf("%.2lf %.2lf %.2lf\n",cur.x,cur.y,ans/2.0);

103     }

104     return 0;

105 }

 

你可能感兴趣的:(memory)