TravelOnMars
SRM 583 · 2012-12-13 · by blue.boy
Problem Statement
Bob recently went to Mars.
There are N cities on Mars. The cities all lie on a circular railroad and they are numbered 0 through N-1 along the railroad. More precisely, there is a railroad segment that connects cities N-1 and 0, and for each i (0 <= i <= N-2) there is a railroad segment that connects cities i and i+1. Trains travel along the railroad in both directions.
You are given a
You are also given
Constraints
- range will contain N elements, where N is between 2 and 50, inclusive.
- Each element of range will be between 1 and 50, inclusive.
- startCity will be between 0 and N-1, inclusive.
- endCity will be between 0 and N-1, inclusive.
- startCity and endCity will be different.
{2,1,1,1,1,1}
1
4
Returns: 2
Bob wants to get from city 1 to city 4. The optimal solution is to travel from city 1 to city 0, and then (as range[0]=2) from city 0 to city 4.
{2,1,1,1,1,1}
4
1
Returns: 3
This is the same test case as Example 0, only startCity and endCity have been swapped. Note that the answer is now 3 instead of 2.
{2,1,1,2,1,2,1,1}
2
6
Returns: 3
Bob starts in city 2. There are two optimal routes: (2->3->5->6) and (2->1->0->6).
{3,2,1,1,3,1,2,2,1,1,2,2,2,2,3}
6
13
Returns: 4
{2,4,2,3,4,1,4,2,5,4,3,3,5,4,5,2,2,4,4,3,3,4,2,3,5,4,2,4,1,3,2,3,4,1,1,4,4,3,5,3,2,1,4,1,4}
24
8
Returns: 5
Submissions are judged against all 121 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TravelOnMars with a public method int minTimes(vector<int> range, int startCity, int endCity) · 121 test cases · 2 s / 256 MB per case