首页 > 代码库 > SOJ - 11512

SOJ - 11512

11512. Big Circle

Constraints

Time Limit: 2 secs, Memory Limit: 256 MB

Description

 

On the opening ceremony of World Cup there was a part where many kids from around the world was trying to make a big circle on the field which symbolized tolerance and multicultural friendship.

They succeed in making a perfect circle, but as they didn‘t practice very much, kids weren‘t uniformly distributed on circle. You spotted that very quickly, and you want to know what is the minimum distance between some two kids.

 

Input

 

First line of the input contains number N (2<=N<=10^5) representing number of kids. Each of next N lines contains two real numbers rounded on two decimal places – coordinates of the each kid. All coordinates will be in interval [-10^6, 10^6]. It is guaranteed that all points will be on circle.

 

 

Output

 

First and only line of output should contain one real number (rounded on two decimal places) – Euclidian distance between two nearest kids. Euclidian distance between points (x1, y1) and (x2, y2) is sqrt((x1-x2)^2+(y1-y2)^2).

 

Sample Input

51.00 4.00-0.50 -1.604.00 1.003.12 3.12-1.60 -0.50

Sample Output

1.56

Hint

 

In the sample, Kids at points (−0.50,−1.60) and (−1.60,−0.50) are nearest and distance between them is 1.56.

 

Problem Source

2014年每周一赛第七场/JBOI 2014

 

题意:求圆上若干点形成的最短弦长度。

思路:先任选三点确定圆心(两弦中垂线交点),再将整个圆平移,使圆心和坐标原点重合,这样就可以从x轴正半轴开始按逆时针将各个点排序,然后再求相邻两点所形成的弦的长度,取最小的就是所求答案。

 

 1 // Problem#: 11512 2 // Submission#: 3027891 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include<bits/stdc++.h> 7 using namespace std; 8 typedef long long ll; 9 struct P{10     double x,y;11 };12 vector<P>v;13 int n;14 double X,Y;15 inline double d2(P a,P b)16 {17     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);18 }19 int F(P p)20 {21     if(p.y == 0)return p.x > 0 ? 0 : 4;22     if(p.x == 0)return p.y > 0 ? 2 : 6;23     if(p.x > 0 && p.y > 0)return 1;24     if(p.x < 0 && p.y > 0)return 3;25     if(p.x < 0 && p.y < 0)return 5;26     if(p.x > 0 && p.y < 0)return 7;27 }28 bool cmp(const P& a,const P& b)29 {30     int fa = F(a),fb = F(b);31     if(fa != fb)return fa < fb;32     else return a.y * b.x < a.x * b.y;33 }34 int main()35 {36     int i;37     while(~scanf("%d",&n))38     {39         v.clear();40         for(i=0;i<n;i++)41         {42             P p;43             scanf("%lf%lf",&p.x,&p.y);44             v.push_back(p);45         }46         if(n==2)47         {48             printf("%.2f\n",sqrt(d2(v[0],v[1])));49             continue;50         }51         double xm01=(v[0].x+v[1].x)/2.0,ym01=(v[0].y+v[1].y)/2.0;52         double xm12=(v[1].x+v[2].x)/2.0,ym12=(v[1].y+v[2].y)/2.0;53         double dym01m12 = ym12 - ym01;54         double dx01=v[1].x-v[0].x,dy01=v[1].y-v[0].y;55         double dx12=v[2].x-v[1].x,dy12=v[2].y-v[1].y;56         X = (dym01m12 * dy12 * dy01 - xm01 * dx01 * dy12 + xm12 * dx12 * dy01) / (dx12 * dy01 - dx01 * dy12);57         if(dy01)58         Y = ym01 - (X - xm01) * dx01 / dy01;59         else//dy12 != 060         Y = ym12 - (X - xm12) * dx12 / dy12;61         for(i=0;i<n;i++)62         {63             v[i].x-=X;64             v[i].y-=Y;65         }66         sort(v.begin(),v.end(),cmp);67         double ans=d2(v[0],v[1]);68         double d;69         for(i=1;i<n;i++)ans = min(ans,d=d2(v[i-1],v[i]));70         ans = min(ans,d2(v[n-1],v[0]));71         printf("%.2f\n",sqrt(ans));72     }73     return 0;74 }75 /*76 377 0.71 0.7178 0.71 -0.7179 -1.00 0.0080 */                                 

 

PS:原题数据貌似不够强,去掉第70行代码也能AC,实际上过不了上面那组数据。

SOJ - 11512