TopCoder problem "Swimmers" used in SRM 169 (Division II Level One)



Problem Statement

    A group of swimmers are stretching along the shores of a river. In just a few moments, they are all going to plunge in, swim downstream (with the current) for a specific distance (unique to each swimmer), and return back (against the current) to their starting points. Relative to a person on the shore, the speed at which each swimmer travels when swimming downstream is the swimmer's speed plus the speed of the current, and the speed at which each swimmer travels when swimming upstream is the swimmer's speed minus the speed of the current. Assume that no time elapses when a swimmer turns around to make the return trip.



Write a class Swimmers with a method getSwimTimes which takes a int[] distances (meters relative to the shore) each swimmer will be swimming, a int[] speeds (meters per second) at which they swim, and an int current representing the speed (meters per second) of the current in the river. Element i in distances corresponds to element i in speeds. The method should return a int[] of the times (rounded down to the nearest integer less than or equal to the actual value) that the roundtrip swim took for each swimmer, or "-1" if the trip is impossible to make. Element i in the returned int[] should correspond to element i in distances and speeds.
 

Definition

    
Class:Swimmers
Method:getSwimTimes
Parameters:int[], int[], int
Returns:int[]
Method signature:int[] getSwimTimes(int[] distances, int[] speeds, int current)
(be sure your method is public)
    
 

Constraints

-distances contains between 0 and 50 elements, inclusive
-each element of distances will be between 0 and 10000, inclusive
-speeds will have the same number of elements as distances
-each element of speeds will be between 0 and 100, inclusive
-current will be between 0 and 100, inclusive
 

Examples

0)
    
{ 300, 300, 300 }
{ 1, 2, 3 }
2
Returns: { -1,  -1,  360 }
The current is 2 meters per second, and each swimmer is going to swim 300 meters with the current followed by 300 meters back to their starting point. The first swimmer only swims at 1 meter per second, so it will be impossible to return to the starting point. The second swimmer matches the speed of the current, but will never be able to travel the return trip. The third swimmer swims at 3 meters per second, so will travel the 300 meters at 5 meters per second with the current and 1 meter per second against the current. This results in 60 seconds for the downstream trip and 300 seconds for the upstream trip, for a total of 360 seconds.
1)
    
{ 500, 500 }
{ 4, 5 }
2
Returns: { 333,  238 }
The first swimmer travels 500 meters downstream at 6 meters/second, resulting in 83 and 1/3 seconds. The upstream trip is done at 2 meters/second and takes 250 seconds. This results in a total time of 333 and 1/3 seconds. The integer portion of this is 333 seconds. The second swimmer travels 500 meters downstream at 7 meters/sec and takes 71 and 3/7 seconds. The upstream trip is done at 3 meters/second and takes 166 and 2/3 seconds. This results in 238 and 2/21 seconds. The integer portion of this is 238 seconds.
2)
    
{ 0, 0 }
{ 1, 2 }
1
Returns: { 0,  0 }
All the swimmers are swimming a distance of 0 meters, so it takes 0 seconds to finish.
3)
    
{ 0, 1 }
{ 0, 0 }
0
Returns: { 0,  -1 }
Watch out for division by zero!
4)
    
{ 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583,
  7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691,
  7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823,
  7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919 }
{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
  73, 79, 83, 89, 97, 99, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30,
  32, 34, 36, 38, 40, 42, 44, 46, 48, 51 }
6
Returns: 
{ -1,  -1,  -1,  8108,  1950,  1474,  1014,  882,  705,  544,  507,  420,  377,  359,  328,  290,  260,  252,  229,  216,  210,  195,  185,  173,  159,  155,  -1,  -1,  4409,  2413,  1717,  1354,  1127,  969,  852,  764,  692,  635,  585,  543,  507,  476,  449,  424,  402,  383,  365,  349,  334,  314 }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1888

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4650&pm=1888

Writer:

huntergt

Testers:

lbackstrom , brett1479

Problem categories:

Simple Math