Poj1981circle and points (points with the largest unit circle coverage)

Source: Internet
Author: User
Tags acos

Link

O (N ^ 3) practices:

Enumerate any two points as the circle of the string, and then enumerate whether other points are in the circle.

Two functions are used.

Atan2 returns the arc tangent function. It is said that it can avoid some special situations.

 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set>10 using namespace std;11 #define N 31012 #define LL long long13 #define INF 0xfffffff14 const double eps = 1e-8;15 const double pi = acos(-1.0);16 const double inf = ~0u>>2;17 struct point18 {19     double x,y;20     point(double x = 0,double y =0 ):x(x),y(y){}21 }p[N];22 double dis(point a,point b)23 {24     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));25 }26 point getcircle(point p1,point p2)27 {28     point mid = point((p1.x+p2.x)/2,(p2.y+p1.y)/2);29     double angle = atan2(p2.y-p1.y,p2.x-p1.x);30     double d = sqrt(1.0-dis(p1,mid)*dis(p1,mid));31     return point(mid.x+d*sin(angle),mid.y-d*cos(angle));32 }33 int dcmp(double x)34 {35     if(fabs(x)<eps)return 0;36     else return x<0?-1:1;37 }38 int main()39 {40     int i,j,n;41     while(scanf("%d",&n)&&n)42     {43         for(i = 1 ;i <= n; i++)44         scanf("%lf%lf",&p[i].x,&p[i].y);45         int maxz = 1;46         for(i = 1; i <= n; i++)47             for(j = i+1 ; j <= n ;j++)48             {49                 if(dis(p[i],p[j])>2.0) continue;50                 int tmax = 0;51                 point cir = getcircle(p[i],p[j]);52                 for(int g =  1; g <= n ;g++)53                 {54                     if(dcmp(dis(cir,p[g])-1.0)>0)55                     continue;56                     tmax++;57                 }58                 maxz = max(maxz,tmax);59             }60         printf("%d\n",maxz);61     }62     return 0;63 }
View code

O (N ^ 2log (n ))

This is similar to the scanning line practice. The circle is centered on each point, and the circle is obtained by enumeration. the intersection point and angle are saved, sorted by angle, and scanned.

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <stdlib. h> 6 # include <vector> 7 # include <cmath> 8 # include <queue> 9 # include <set> 10 using namespace STD; 11 # define n 31012 # define ll long long13 # define INF 0xfffffff14 const double EPS = 1e-8; 15 const double Pi = ACOs (-1.0); 16 const double INF = ~ 0u> 2; 17 struct point18 {19 Double X, Y; 20 point (double x = 0, Double Y = 0): x (x), y (y) {} 21} p [N]; 22 struct node23 {24 Double Ang; 25 int in; 26} arc [N * n]; 27 double DIS (point a, point B) 28 {29 return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y); 30} 31 int DCMP (Double X) 32 {33 If (FABS (x) <EPS) return 0; 34 else return x <0? -1:1; 35} 36 bool CMP (node A, Node B) 37 {38 If (DCMP (. ang-b.ang) = 0) 39 return. in> B. in; 40 return DCMP (. ang-b.ang) <0; 41} 42 int main () 43 {44 int I, j, N; 45 while (scanf ("% d", & N) 46 {47 for (I = 1; I <= N; I ++) 48 scanf ("% lf", & P [I]. x, & P [I]. y); 49 int G = 0; 50 int ans = 0, maxz = 1; 51 for (I = 1; I <= N; I ++) 52 {53 ans = 0; 54g = 0; 55 for (j = 1; j <= N; j ++) 56 {57 if (DIS (P [I], P [J])> 2.0) continue; 58 double ang1 = atan2 (P [J]. y-P [I]. y, P [J]. x-P [I]. x); 59 double ang2 = ACOs (DIS (P [I], p [J])/2); 60 arc [++ g]. ang = ang1-ang2; // here the angle algorithm is cleverly 61 arc [g]. in = 1; 62 arc [++ g]. ang = ang1 + ang2; 63 arc [g]. in =-1; 64} 65 sort (ARC + 1, arc + G + 1, CMP); 66 67 // cout <G <Endl; 68 for (j = 1; j <= g; j ++) 69 {70 ans + = arc [J]. in; 71 maxz = max (maxz, ANS); 72} 73} 74 printf ("% d \ n", maxz); 75} 76 return 0; 77}
View code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.