2017CCPC哈尔滨 M:Geometry Problem(随机)

题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1013&cid=784


题意:

给你n个点,找出一个圆满足至少有一半(向上取整)以上的点都在这个圆上(注意不是圆内)

答案可能有多种,输出任意一种,保证有解,但是解的圆心坐标和半径绝对值一定小于1e9


思路:

每次随机三个点求出外接圆,然后再判断是否合法

虽然每次成功率只有1/8,但是多几次一定可以找到

这题主要是坑比较多:

①注意精度,还有答案不能超过1e9,

②n<=4要特判,这个时候直接取两个点求个圆

③随机的三个点一定各不相同且不能在同一条直线上

//2017CCPC哈尔滨--M
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct
{
	double x;
	double y;
}Point;
Point s[100005];
int Jud(int a, int b, int c)
{
	if(a==b || a==c || b==c)
		return 1;
	if((s[b].x-s[a].x)*(s[c].y-s[a].y)==(s[b].y-s[a].y)*(s[c].x-s[a].x))
		return 1;
	return 0;
}
int main(void)
{
	double x, y, r;
	int T, n, i, a, b, c, sum;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d", &n);
		for(i=1;i<=n;i++)
			scanf("%lf%lf", &s[i].x, &s[i].y);
		if(n==1)
		{
			printf("%f %f 1.000000\n", s[1].x+1, s[1].y);
			continue;
		}
		if(n<=4)
		{
			printf("%f %f %f\n", (s[1].x+s[2].x)/2, (s[1].y+s[2].y)/2, sqrt((s[1].x-s[2].x)*(s[1].x-s[2].x)+(s[1].y-s[2].y)*(s[1].y-s[2].y))/2);
			continue;
		}
		while(1)
		{
			a = rand()%n+1;
			b = rand()%n+1;
			c = rand()%n+1;
			if(Jud(a, b, c))
				continue;
			x = (s[a].x*s[a].x-s[b].y*s[b].y-s[b].x*s[b].x+s[a].y*s[a].y)*(s[a].y-s[c].y)-(s[a].x*s[a].x-s[c].y*s[c].y-s[c].x*s[c].x+s[a].y*s[a].y)*(s[a].y-s[b].y);
			x /= 2*(s[a].y-s[c].y)*(s[a].x-s[b].x)-2*(s[a].y-s[b].y)*(s[a].x-s[c].x);
			y = (s[a].x*s[a].x-s[b].y*s[b].y-s[b].x*s[b].x+s[a].y*s[a].y)*(s[a].x-s[c].x)-(s[a].x*s[a].x-s[c].y*s[c].y-s[c].x*s[c].x+s[a].y*s[a].y)*(s[a].x-s[b].x);
			y /= 2*(s[a].y-s[b].y)*(s[a].x-s[c].x)-2*(s[a].y-s[c].y)*(s[a].x-s[b].x);
			r = sqrt((s[a].x-x)*(s[a].x-x)+(s[a].y-y)*(s[a].y-y));
			sum = 0;
			for(i=1;i<=n;i++)
			{
				if(fabs(sqrt((s[i].x-x)*(s[i].x-x)+(s[i].y-y)*(s[i].y-y))-r)<1e-6)
					sum++;
			}
			if(sum>=(n+1)/2 && fabs(x)<1e9 && fabs(y)<1e9 && fabs(r)<1e9)
				break;
		}
		if(fabs(x)<=1e-6)
			x = fabs(x);
		if(fabs(y)<=1e-6)
			y = fabs(y);
		printf("%f %f %f\n", x+1e-8, y+1e-8, r+1e-8);
	}
	return 0;
}
/*
125
5
1 5 2 5 7 6 2 3 5 6
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值