Long Distance Racing
InterviewTime limit1sMemory limit128 MB
Given a terrain string and per-unit times, find the farthest segment index k whose round-trip time stays within M seconds.
- Level
Medium4 of 10
- Topics
- Array, Prefix sum, Implementation, Binary search
- Solved
- No attempts yet
Problem
Bessie is training for her next race by running on a path that includes hills, so she will be ready for any terrain. She has chosen a single straight path and wants to run as far from the farm as she can, but she must be back at the farm within seconds ().
The chosen path is units long () and is split into equal-length segments, each of which is uphill, flat, or downhill. Segment is given by a single character , where u means uphill, f means flat, and d means downhill.
Bessie takes seconds to run one unit of uphill (), seconds for one unit of flat (), and seconds for one unit of downhill (). On the way home, every uphill segment becomes downhill and every downhill segment becomes uphill (flat stays flat).
If Bessie runs to the end of segment and returns, she passes segments through in order on the way out and the same segments in reverse on the way back. Find the greatest distance (number of units) she can reach from the farm and still return within seconds.
Input
- Line 1: Five space-separated integers , , , , and .
- Lines 2 through : line contains a single character describing segment .
Output
- A single integer: the greatest distance (number of units) Bessie can reach from the farm and still return within seconds.