Descripton After the closing ceremony of an ICPC competition, Lucas and his friend decided to go to the hotel pool to have some magical drinks. After a while, Lucas found a magic swim ring and also realized that the swim rings had some interesting properties:
The swim rings are perfectly circular. The swim rings do not move. There may be a non-empty intersection between the swim rings in the pool. The magic swim ring can absorb any swim ring it intersects or is tangent to. After absorption, the radius of the magic swim ring will be equal to the sum its current radius and the radius of the swim ring that was absorbed. It was then that Lucas promised that his friend would become a grandmaster (red) at ForceCodes if he was able to solve a challenge. Obviously, his friend accepted the challenge, because he would love to become red and go screaming through the hotel so that everyone knew of his achievement. Since he was not feeling well after drinking some magic drinks, he decided to ask you to help him solve the problem. Assuming the pool is an infinite plane and the swim rings are located at points (Xi, Yi) with radius Ri, Lucas would like to know the smallest possible radius, such that if he places a magic swim ring centered at the point (x, y), all swim rings would be absorbed by the magic ring.
Example: In this example, the magic swim ring (red) would need a radius of at least 1 to absorb the blue swim ring. After absorption, its new radius would be equal to 4, and then it would be able to absorb the purple swim ring.
Input The first line of input contains three integers N (0<N≤105) and x, y (−109<x,y≤109), the number of swim rings and the center of the magic swim ring.
The i-th of the next N lines contains three integers Xi, Yi (−109<Xi,Yi≤109) and Ri(1<Ri≤105), center and radius of the i-th swim ring.
Output You must print the minimum radius the magic swim ring should have in order to solve Lucas’ challenge. Your answer is considered correct if its absolute or relative error does not exceed 10−6.
Examples
Input 2 1 1 1 7 3 5 1 3
Output 1.0000000000
Input 4 211 -458 335 369 6 -771 -753 30 193 -617 41 -27 -396 78
Output 870.3531099090
解法 先求出每个圆到给定点的距离 - 半径 的值, 再根据该值从小到大排序 从小到大枚举所有的圆,贪心的求出吸收当前圆所需要的最小初始半径
Code
#include <bits/stdc++.h> using namespace std; const int MX = 2e5 + 7; struct circle{ double x,y; double r; double dis; inline bool operator<(const circle&it)const{ return dis < it.dis; } }cir[MX]; double cal(double x1,double y1,double x2,double y2){ return sqrt(1.0 * (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } double ans, sum; int main(){ int n,X,Y;scanf("%d %d %d",&n,&X,&Y); for(int i = 1;i <= n;++i){ scanf("%lf %lf %lf",&cir[i].x,&cir[i].y,&cir[i].r); cir[i].dis = cal(cir[i].x,cir[i].y,X,Y) - 1.0 * cir[i].r; } sort(cir + 1,cir + 1 + n); ans = cir[1].dis; sum = 1.0 * cir[1].r + ans; for(int i = 2;i <= n;++i){ if(cir[i].dis >= sum) ans += cir[i].dis - sum, sum = cir[i].dis + 1.0 * cir[i].r; else sum += 1.0 * cir[i].r; } printf("%.12f\n", ans); return 0; }