poj1981Circle and Points(单位圆覆盖最多的点)

链接

O(n^3)的做法:

枚举任意两点为弦的圆,然后再枚举其它点是否在圆内。

用到了两个函数

atan2反正切函数,据说可以很好的避免一些特殊情况

 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 310
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 struct point
18 {
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 }

O(n^2log(n))

这个类似扫描线的做法,以每一个点为圆心化圆,枚举与其相交得圆,保存交点和角度,按角度排序后,扫一遍。

 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 310
12 #define LL long long
13 #define INF 0xfffffff
14 const double eps = 1e-8;
15 const double pi = acos(-1.0);
16 const double inf = ~0u>>2;
17 struct point
18 {
19     double x,y;
20     point(double x = 0,double y =0 ):x(x),y(y) {}
21 } p[N];
22 struct node
23 {
24     double ang;
25     int in;
26 } arc[N*N];
27 double dis(point a,point b)
28 {
29     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.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(a.ang-b.ang)==0)
39         return a.in>b.in;
40     return dcmp(a.ang-b.ang)<0;
41 }
42 int main()
43 {
44     int i,j,n;
45     while(scanf("%d",&n)&&n)
46     {
47         for(i = 1 ; i <= n; i++)
48             scanf("%lf%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;
54             g = 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;//这里角度的算法很巧妙
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 }

poj1981Circle and Points(单位圆覆盖最多的点)

时间: 2024-10-09 13:44:38

poj1981Circle and Points(单位圆覆盖最多的点)的相关文章

poj1981 Circle and Points 单位圆覆盖问题

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Circle and Points Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 6850   Accepted: 2443 Case Time Limit: 2000MS Description You are given N points in the xy-plane. You have a cir

单位圆的最多覆盖据点 (重点需要学习!)

我自己的代码,提交错误,需要修改 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <iostream> #include <string> #include <algorithm> #define eps 1e-8 using namespace st

hdu 1077(单位圆覆盖问题)

Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1741    Accepted Submission(s): 686 Problem Description Ignatius likes catching fish very much. He has a fishnet whose shape is a c

Java for LeetCode 149 Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

POJ 题目1106 Transmitters(数学几何)

Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4756   Accepted: 2538 Description In a wireless network with multiple transmitters sending on the same frequencies, it is often a requirement that signals don't overlap, or at

HDU 1077 - Catching Fish

Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1827    Accepted Submission(s): 725 Problem Description Ignatius likes catching fish very much. He has a fishnet whose shape is a c

洛谷 P2038 无线网络发射器选址(NOIp2014D2T1)

题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻的平行街道之间的距离都是恒定值 1 .东西向街道从北到南依次编号为0,1,2-128 , 南北向街道从西到东依次编号为0,1,2-128 . 东西向街道和南北向街道相交形成路口,规定编号为x 的南北向街道和编号为y 的东西向街道形成的路口的坐标是(x , y ). 在 某 些 路口存在一定数量的公共

【NOIP之旅】NOIP2014 day2 T1 无线网络发射器选址

1.无线网络发射器选址 (wireless.cpp/c/pas) [问题描述] 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129条东西向街道和129条南北向街道所形成的网格状,并且相邻的平行街道之间的距离都是恒定值1.东西向街道从北到南依次编号为0,1,2…128,南北向街道从西到东依次编号为0,1,2…128. 东西向街道和南北向街道相交形成路口,规定编号为x的南北向街道和编号为y的东西向街道形成的路口的坐标是(

洛谷2038 无线网络发射器选址

题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网.假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻的平行街道之间的距离都是恒定值 1 .东西向街道从北到南依次编号为0,1,2…128 , 南北向街道从西到东依次编号为0,1,2…128 . 东西向街道和南北向街道相交形成路口,规定编号为x 的南北向街道和编号为y 的东西向街道形成的路口的坐标是(x ,  y ). 在 某 些 路口存在一定数量的公共