Overtaking
Time limit8sMemory limit1024 MB
Given each runner's distance per minute, count how many times the runner who was behind takes the lead.
- Level
Easy3 of 10
- Topics
- Simulation, Array
- Solved
- No attempts yet
Problem
Two athletes run a long-distance race, but the race is different from usual athletics events. They run for a predetermined duration, and the one who runs longer is the winner. At every minute after the start, the distances both runners ran in the previous one minute are recorded. Write a program that, given the record of a race, counts the number of overtakings by either runner during the race. Assume that both runners keep constant paces for every one minute before their distances are recorded.
Here, an overtaking is an event where the runner previously behind takes the lead. Before an overtaking, the two runners may run side by side for a while, during which neither takes the lead. Figure B-1 shows the times and positions of two runners for the third dataset of Sample Input. The number of overtakings in this case is two.

Figure B-1: The third dataset of Sample Input
Input
The input consists of multiple datasets, each in the following format.
is an integer between 2 and 1000, inclusive. It is the duration of the race in minutes. Each () is an integer between 1 and 1000, inclusive. It is the distance in meters that the first runner ran between minutes and minutes after the start. Similarly, () is the distance the second runner ran in the same interval.
The end of the input is a line containing a single zero. The number of datasets does not exceed 100.
Output
For each dataset, output the number of overtakings on a line.