ShortCut
SRM 215 · 2004-10-16 · by lbackstrom
Problem Statement
Given a section of one-way road that is a sequence of straight-line segments, I want to know
the fastest way to get from the start of the section to the end of the section. I can
leave and re-enter the road as often as I wish.
Create a method suvTime that is given a
The i-th elements of roadX and roadY give the coordinates of i-th point along the road, where the first elements gives the start point and the last elements give the end point.
Constraints
- roadX will contain between 2 and 50 elements inclusive.
- roadY will contain the same number of elements as roadX.
- Each element in roadX and in roadY will be between -1000 and 1000 inclusive.
- No two road segments will touch or intersect (except that the last point of a segment is the first point of the next segment).
{10,14,14}
{0,0,4}
Returns: 1.0
E x x x Sxxxx 'S' is the start point, 'E' the end, and 'x' shows the road. Cross country might be fun, but leaving the road in this case would be a mistake. It could shorten the distance of the trip, but it would increase the time taken.
{0,4,4}
{0,4,-4}
Returns: 0.8001991549907409
x xx x x x x S x \ x \ x x E The \ shows the appropriate shortcut, which is directly from the start point to a little south on the final segment. This off-road leg has length 4.6188 and rejoins the road at a point where the remaining on-road travel has length 1.7. The original road distance was about 13.66 (5.66 northeastward, 8 southward). The shortcut reduces the trip to an equivalent road distance of 2*4.6188 + 1.7 = 10.93 which requires only about 80% of the time required by those pitiful non-SUV owners.
{0,10, 10,-40,-40,5, 5,150,150,160,160,150,150}
{0, 0,-10,-10,50,5,50, 50, 60, 60,-10,-10, 0}
Returns: 0.5840318810924577
{0,10, 10,-40,-40,5, 5,150,150,160,160,150,150}
{0, 0,-10,-10,50,5,50, 50, 60, 60, 0, 0, 10}
Returns: 0.564350562948024
{0, 0,-100,-100,100,100,-20,-20}
{0,-10, -10, 5, 5, 20, 20, 6}
Returns: 0.08810385239586963
Submissions are judged against all 33 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ShortCut with a public method double suvTime(vector<int> roadX, vector<int> roadY) · 33 test cases · 2 s / 256 MB per case