poj1981Circle and Points(单位圆覆盖最多的点)
O(n^3)的做法:
枚举任意两点为弦的圆,然后再枚举其它点是否在圆内。
用到了两个函数
atan2反正切函数,据说可以很好的避免一些特殊情况
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 310
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y){}
}p[N];
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
point getcircle(point p1,point p2)
{
point mid = point((p1.x+p2.x)/,(p2.y+p1.y)/);
double angle = atan2(p2.y-p1.y,p2.x-p1.x);
double d = sqrt(1.0-dis(p1,mid)*dis(p1,mid));
return point(mid.x+d*sin(angle),mid.y-d*cos(angle));
}
int dcmp(double x)
{
if(fabs(x)<eps)return ;
else return x<?-:;
}
int main()
{
int i,j,n;
while(scanf("%d",&n)&&n)
{
for(i = ;i <= n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
int maxz = ;
for(i = ; i <= n; i++)
for(j = i+ ; j <= n ;j++)
{
if(dis(p[i],p[j])>2.0) continue;
int tmax = ;
point cir = getcircle(p[i],p[j]);
for(int g = ; g <= n ;g++)
{
if(dcmp(dis(cir,p[g])-1.0)>)
continue;
tmax++;
}
maxz = max(maxz,tmax);
}
printf("%d\n",maxz);
}
return ;
}
O(n^2log(n))
这个类似扫描线的做法,以每一个点为圆心化圆,枚举与其相交得圆,保存交点和角度,按角度排序后,扫一遍。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 310
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y) {}
} p[N];
struct node
{
double ang;
int in;
} arc[N*N];
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int dcmp(double x)
{
if(fabs(x)<eps)return ;
else return x<?-:;
}
bool cmp(node a,node b)
{
if(dcmp(a.ang-b.ang)==)
return a.in>b.in;
return dcmp(a.ang-b.ang)<;
}
int main()
{
int i,j,n;
while(scanf("%d",&n)&&n)
{
for(i = ; i <= n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
int g = ;
int ans = ,maxz = ;
for(i = ; i <= n ; i++)
{
ans = ;
g = ;
for(j = ; j <= n ; j++)
{
if(dis(p[i],p[j])>2.0) continue;
double ang1 = atan2(p[j].y-p[i].y,p[j].x-p[i].x);
double ang2 = acos(dis(p[i],p[j])/);
arc[++g].ang = ang1-ang2;//这里角度的算法很巧妙
arc[g].in = ;
arc[++g].ang = ang1+ang2;
arc[g].in = -;
}
sort(arc+,arc+g+,cmp); //cout<<g<<endl;
for(j = ; j <= g;j++)
{
ans+=arc[j].in;
maxz = max(maxz,ans);
}
}
printf("%d\n",maxz);
}
return ;
}
poj1981Circle and Points(单位圆覆盖最多的点)的更多相关文章
- bzoj1338: Pku1981 Circle and Points单位圆覆盖
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1338 1338: Pku1981 Circle and Points单位圆覆盖 Time ...
- POJ-1981 Circle and Points 单位圆覆盖
题目链接:http://poj.org/problem?id=1981 容易想到直接枚举两个点,然后确定一个圆来枚举,算法复杂度O(n^3). 这题还有O(n^2*lg n)的算法.将每个点扩展为单位 ...
- poj1981 Circle and Points 单位圆覆盖问题
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Circle and Points Time Limit: 5000MS Me ...
- Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】
<题目链接> <转载于 >>> > 题目大意: 在二维平面上给出n条不共线的线段(线段端点是整数),问这些线段总共覆盖到了多少个整数点. 解题分析: 用GC ...
- poj 1981(单位圆覆盖最多点问题模板)
Circle and Points Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 7327 Accepted: 2651 ...
- hdu 1077(单位圆覆盖问题)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- poj 1981 Circle and Points
Circle and Points Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 8131 Accepted: 2899 ...
- 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. ...
随机推荐
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- Discuz! X2头部header.htm修改指南
相对于1.5版本,2.0版本并没有在模板机制上做太大改动,基本延续了1.5的模板机制.下面我就为和大家一起过一下X2的头部代码.希望大家对头部代码有些认识. 1.顶部的设为首页,和收藏本站.这里是可以 ...
- Browser对象
Window对象即浏览器中打开的窗口,当文档里面有框架(frame或者iframe标签)时,浏览器会为HTML文档创建一个window对象,并为每个框架创建一个额外的window对象. 属性close ...
- javaWEB小练习:在数据库中查找相同的username和password
/*练习题: * 在Mysql数据库中创建一个person数据表,添加三个字段,id,user,password,并录入几条记录 * *练习题:定义一个login.html,里面定义了两个请求字段:u ...
- java单元测试(Junit)
Eclipse最基本的模块测试 1:首先创建一个java工程,在工程中创建一个被单元测试的Student数据类,如下: package UnitTest; public class Student { ...
- 使用repeater实现gridview的功能
<asp:Repeater ID="rptfindData" runat="server"> <HeaderTemplate> < ...
- linux epoll 学习
一.epoll介绍 epoll是linux内核为处理大批量句柄而作的改进的poll,是linux下IO多路复用select.poll的增强版,它能显著减少程序在大量并发连接中只有少量活跃的情况下的系统 ...
- winform中利用反射实现泛型数据访问对象基类(2)
在1的基础上做了一点改进 参数化处理 看上去更简洁 无主键情况下 update 方法需要改进 insert delete没有问题 /// <summary> /// DAO基类 ...
- lua注释
1. 单行注释 -- 功能等同于C++中的// 2. 多行注释 --[[ 注释的内容 ]] 功能等同于C++中的 /**/ 3. 多行注释 --[====[ 注释和内容 ]=== ...
- Objective-C之null NaN undefined
http://blog.csdn.net/siemenliu/article/details/6568306