Codeforces Round #465 (Div. 2) C. Fifa and Fafa(圆问题,数论)

C. Fifa and Fafa
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius R. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.

The world is represented as an infinite 2D plane. The flat is centered at (x1, y1) and has radius R and Fafa's laptop is located at (x2, y2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area.

Input

The single line of the input contains 5 space-separated integers R, x1, y1, x2, y2 (1 ≤ R ≤ 105|x1|, |y1|, |x2|, |y2| ≤ 105).

Output

Print three space-separated numbers xap, yap, r where (xap, yap) is the position which Fifa chose for the access point and r is the radius of its range.

Your answer will be considered correct if the radius does not differ from optimal more than 10 - 6 absolutely or relatively, and also the radius you printed can be changed by no more than 10 - 6 (absolutely or relatively) in such a way that all points outside the flat and Fafa's laptop position are outside circle of the access point range.

Examples
input
Copy
5 3 3 1 1
output
3.7677669529663684 3.7677669529663684 3.914213562373095
input
Copy
10 5 5 5 15
output
5.0 5.0 10.0

题目大意:现在有一个半径为r,圆心为(x1,y1)的圆,和一个(x2,y2)的点,现在要求规定圆内找一个点形成一个圆使得点在圆外(或圆上)且使得规定圆的范围尽可能和圆覆盖

解题思路:根据题意,(x2,y2)这个点有两种情况,一种在规定圆内部,一种是在规定圆外部,在外部的话,要求算的圆其实就是规定的圆,如果在规定圆的内部要分两种情况

一种是正好是圆心,这样的话,我们就可以在圆心到圆上画一个半径为r/2的圆,因为要考虑到使得规定圆与要求的圆尽量重合,那么点在要求圆上是最好的选择

还有一种是不再圆心上,那我们要求圆的圆心一定是在通过点和规定圆圆心的射线上,以点为段的射线上,那么半径R就是r+点与规定圆的距离了,至于求要求圆的圆心,我们可以根据相似三角形的原则,利用算出的半径R和圆心到点的距离,相似两个三角形,一个三角形是规定圆与点的三角形,一个是要求圆圆点与点形成的三角形即可

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;

LL x1,x2,r,yy1,y2;
double rr,rx,ry;
int main()
{
	scanf("%lld %lld %lld %lld %lld", &r, &x1, &yy1, &x2, &y2);
	if(x1==x2 && yy1==y2)
	{
		printf("%.10lf %.10lf %.10lf\n",double(x1*1.0+r/2.0),double(yy1*1.0),double(r/2.0));
		return 0;
	}
	if((x1-x2)*(x1-x2)+(yy1-y2)*(yy1-y2)>=r*r)
	{
		printf("%0.10lf %0.10lf %0.10lf\n",double(x1*1.0),double(yy1*1.0),double(r*1.0));
		return 0;
	}
	//以下上相似三角形的算法
	rr= (sqrt((x1-x2)*(x1-x2)+(yy1-y2)*(yy1-y2)) + r)/2.0;
	rx = double(x1-x2);
	ry = double(yy1-y2);
	rx = rr/sqrt((x1-x2)*(x1-x2)+(yy1-y2)*(yy1-y2))*rx + x2*1.0;
	ry= rr/sqrt((x1-x2)*(x1-x2)+(yy1-y2)*(yy1-y2))*ry + y2*1.0;
	printf("%0.10lf %0.10lf %0.10lf\n",rx,ry,rr);
	return 0;
}

你可能感兴趣的:(ACM,ACM算法,数论)