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. ...
随机推荐
- iOS 判断相机权限是否被限制,判断相机是否可以使用
判断相机权限是否被限制 需要导入 AVFoundation 类 [objc] view plain copy #import <AVFoundation/AVFoundation.h> ...
- class打包成exe方式
首先运行dos命令 jar -cvf **.jar *.class 把所有文件打包成jar包 用解压器打开jar包 打开meta-inf文件夹 打开用记事本打开main.mf文件 增加语句Main-C ...
- 判断sql执行效率以及针对临时表的使用
/****** Script for SelectTopNRows command from SSMS ******/ SET STATISTICS io ON SET STATISTICS time ...
- zoj 3557 How Many Sets II
How Many Sets II Time Limit: 2 Seconds Memory Limit: 65536 KB Given a set S = {1, 2, ..., n}, n ...
- Android 内存分析工具 - LogCat GC
一.GC_Reason 触发垃圾回收的回收的集中原因: 类型 描述 GC_CONCURRENT 内存使用将满时,并发的进行垃圾回收. GC_FOR_MALLOC 当内存已满应用尝试分配内存时会出触发垃 ...
- 数的n次方 s.match(reg) marquee滚动效果
一.数的n次方 <script> alert(math.pow(a,5)); /*输出a的5次方*/ </script> 二. s.match(reg); s代表一个字符串,r ...
- 定时自动启动任务crontab命令用法
crontab简介 crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行.该词 ...
- Cheatsheet: 2013 07.09 ~ 07.20
Mobile How to implement Android Splash Screen Migrating iOS MVC Applications to Windows Phone 8 (M-V ...
- FreeSWITCH第三方库(其他)的简单介绍(三)
FreeSWITCH使用了大量的第三方库,本文档主要介绍关联相关库的信息: 音频相关库的信息介绍参考:http://www.cnblogs.com/yoyotl/p/5486753.html 视频相关 ...
- 关于MySQL数据库如何按时间查询
这里做了几个测试 select * from simingpai where TIMESTAMP(createTime) >= '2015-9-6'; select * from simingp ...