Connection Status:
Competition Arena > MagicalGirlLevelOneDivTwo
SRM 514 · 2011-05-25 · by wrong · Simple Math
Class Name: MagicalGirlLevelOneDivTwo
Return Type: double
Method Name: theMinDistance
Arg Types: (int, int, int)
Problem Statement

Problem Statement

Magical Girls are girls who have magical powers. They fight against evil witches to protect the Earth.


You, one of the Magical Girls, are now at point (0, 0) on the plane. You found an evil witch at point (x, y), so you will attack her by Attack Magic.


The range of your Attack Magic is a square with side length 2d centered at your location. That is, if you are at point (u, v), the square which indicates the range of your magic has four vertices (u+d, v+d), (u-d, v+d), (u-d, v-d) and (u+d, v-d).


Before casting Attack Magic, you may move to any point in the plane. Return the minimum distance you have to move so that the evil witch is inside or on the edge of the square.

Notes

  • The distance between two points (x1, y1) and (x2, y2) is sqrt((x2-x1)^2 + (y2-y1)^2).
  • The returned value must have an absolute or relative error less than 1e-9.

Constraints

  • d will be between 1 and 500, inclusive.
  • x and y will each be between -500 and 500, inclusive.
Examples
0)
5
10
10
Returns: 7.0710678118654755

Moving to (5, 5) achieves the minimum distance. The distance is sqrt(5*5 + 5*5) = 7.0710678118654755.

1)
5
10
3
Returns: 5.0

Moving to (5, 0) achieves the minimum distance. The distance is sqrt(5*5 + 0*0) = 5.

2)
5
-8
-6
Returns: 3.1622776601683795
3)
5
3
2
Returns: 0.0

The evil witch is already inside of the square.

4)
24
24
-24
Returns: 0.0

Submissions are judged against all 110 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class MagicalGirlLevelOneDivTwo with a public method double theMinDistance(int d, int x, int y) · 110 test cases · 2 s / 256 MB per case

Submitting as anonymous