题目链接:点我
题目
The center coordinate of the circle C is O, the coordinate of O is (0,0) , and the radius is r.
P and Q are two points not outside the circle, and PO = QO.
You need to find a point D on the circle, which makes PD+QD minimum.
Output minimum distance sum.
Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with r : the radius of the circle C.
Next two line each line contains two integers x , y denotes the coordinate of P and Q.
Limits
T≤500000
−100≤x,y≤100
1≤r≤100
Output
For each case output one line denotes the answer.
The answer will be checked correct if its absolute or relative error doesn't exceed 10−6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if |a−b|max(1,b)≤10−6.
Sample Input
4
4
4 0
0 4
4
0 3
3 0
4
0 2
2 0
4
0 1
1 0
Sample Output
5.6568543
5.6568543
5.8945030
6.7359174
Source
2017 Multi-University Training Contest - Team 6
题意:
在圆上找一点 D, 算PD + QD 的最小值,
思路:
比赛时想到了在中垂线上的情况,如何写出来发现样例只能过后两个,于是就想是不是自己想错了,当时又想到高中时平面几何的类似方法,找出点关于圆的的边界的对称点,然后两点之间直线最短.然而由于太复杂,没有写出来,而且当时还没开始学计算几何,不知道反演(维基百科)是什么东西,待看到题解后发现是反演然后加上相似三角形就可以判了,
官方题解:
很不幸不总是中垂线上的点取到最小值,考虑点在圆上的极端情况。
做P点关于圆的反演点P',OPD与ODP'相似,相似比是|OP| : r。
Q点同理。
极小化PD+QD可以转化为极小化P'D+Q'D。
当P'Q'与圆有交点时,答案为两点距离,否则最优值在中垂线上取到。
时间复杂度 O(1)O(1)O(1)
也有代数做法,结论相同。
优秀的黄金分割三分应该也是可以卡过的。
在图上做出反演点并连上三角形,我们得到 OP’ / OD = r / op,这是我们判断直线P’Q’与圆是否有交点,显然直线上离圆最近的点是线段P’Q’的中点:
当有交点是我们可以算到P’D + Q’D 的最小值,根据相似三角形 PD + QD = (P’D+ Q’D);
如果没有交点 ,那么根据三角形DP’Q’三边的关系我们知道P’D + Q’D 不是最优解,而此时最优解就是中垂线与圆交点得到的D,
相似的性质巧妙运用可以大大简化计算.
代码:
1 |
|