Sleeping on the Train
Time limit2sMemory limit2048 MB
Given a train that shuttles between stops 1 and n, find the fewest turnarounds it makes while Antonio rides from stop a to stop b, using t sleep-wake sightings to pin its position.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Simulation
- Solved
- No attempts yet
Problem
Antonio is sightseeing in Line Town. As part of the trip, he rides the famous Line Train. The train serves stops, numbered 1 to . It starts at stop 1 and moves through the stops in increasing order until it reaches stop . There it turns around and moves through the stops in decreasing order until it reaches stop 1, where it turns around again and repeats the journey.
When the train arrives at stop 1 or stop , all passengers who want to get off leave the train. Then the train turns around, and new passengers board before it heads to the next stop.
Antonio travels from stop to stop . He is very sleepy and does not check the direction of the train when he boards, so he may board a train that is heading the wrong way. Right after he boards, he falls asleep. He wakes up times during the trip. Each time, he sees that the train is between stop and stop . He cannot tell which direction the train is moving. Since he is not at his destination yet, he falls back asleep right away.
After the -th time he wakes up, Antonio stays awake for the rest of the trip. He remains on the train until it next stops at stop , and then he gets off.
Find the minimum number of times the train turned around while Antonio was on it.
Input
The first line contains four integers (), (), , and (, ).
The second line contains integers. The -th integer () means that when Antonio woke up for the -th time, the train was between stop and stop .
Output
Print the minimum number of times the train turned around while Antonio was on it.