VariableSpeedLimit
SRM 393 · 2008-03-11 · by StevieT
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
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.
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.
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.
1000
{50,40,30,40,50}
Returns: 24.0
2058
{80,43,57,23,28,45,60,75,73,80}
Returns: 37.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.
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