Connection Status:
Competition Arena > VariableSpeedLimit
SRM 393 · 2008-03-11 · by StevieT · Search, Simple Math, Simulation
Class Name: VariableSpeedLimit
Return Type: double
Method Name: journeyTime
Arg Types: (int, vector<int>)
Problem Statement

Problem Statement

In order to make its roads safer, the government of a particular country has decided to introduce a speed limit that varies throughout the day. At busy times, the speed limit will decrease to reduce the risk of a dangerous accident. You need to drive a distance of journeyLength units and want to know how long it will take you.

You are given details of the speed limit in a int[] speedLimit. Element i (zero-based) gives the speed limit in DISTANCE UNITS/TIME UNITS that is in force between times T = i and T = i + 1. speedLimit describes the speed limit for a full day, so after this the pattern repeats (i.e., if N is the number of elements in speedLimit, the speed limit between times N and N+1 is given by speedLimit[0], etc.). You start your journey at time T = 0 and should assume that you travel exactly at the speed limit for your entire journey. Return a double containing the amount of time it takes to complete your journey.

Notes

  • Your return value must be accurate to an absolute or relative tolerance of 1E-9.

Constraints

  • journeyLength will be between 1 and 100000 (10^5), inclusive.
  • speedLimit will contain between 1 and 50 elements, inclusive.
  • Each element of speedLimit will be between 1 and 100, inclusive.
Examples
0)
100
{50}
Returns: 2.0

Here the speed limit doesn't change. The journey therefore takes a time of DISTANCE/SPEED = 100/50 = 2 time units.

1)
100
{50,25}
Returns: 2.5

Now the speed limit drops to 25 every other time unit. You drive 50 units in the first time unit and 25 in the second. You reach the end of your journey halfway through the third time unit.

2)
1000
{50,40,30,40,50}
Returns: 24.0
3)
2058
{80,43,57,23,28,45,60,75,73,80}
Returns: 37.4
4)
56935
{82,20,17,15,48,3,9,64,98,84,81,53,32,20}
Returns: 1272.65625

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

Coding Area

Language: C++17 · define a public class VariableSpeedLimit with a public method double journeyTime(int journeyLength, vector<int> speedLimit) · 47 test cases · 2 s / 256 MB per case

Submitting as anonymous