ReturnToHome
SRM 318 · 2006-08-29 · by Andrew_Lazarev
Problem Statement
You are located at point (X, Y) and you want to get home to point (0, 0) as quickly as possible. There are two allowable methods of movement. The first method is walking, and you can walk at a speed of 1 unit per second. The second method is jumping, and you can jump D units in T seconds. You can jump in any straight direction, but you can only jump exactly D units. Return the minimum number of seconds required to get home using these two methods. You are not limited to using just one method for the entire trip; you can use any combination of walking and jumping.
Notes
- Your return value must have an absolute or relative error less than 1e-9.
Constraints
- X will be between 1 and 1000, inclusive.
- Y will be between 1 and 1000, inclusive.
- D will be between 1 and 10000, inclusive.
- T will be between 1 and 10000, inclusive.
6 8 5 3 Returns: 6.0
The fastest way to get home is by using two jumps.
3 4 6 3 Returns: 4.0
You should jump one unit past your destination and then walk back.
400 300 150 10 Returns: 40.0
You should get home using four jumps.
318 445 1200 800 Returns: 546.9451526432975
The fastest way is to walk.
387 717 413 192 Returns: 384.0
6 8 3 2 Returns: 7.0
Make three jumps and then walk the remaining 1 unit.
Submissions are judged against all 138 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ReturnToHome with a public method double goHome(int X, int Y, int D, int T) · 138 test cases · 2 s / 256 MB per case