Running Laps
Time limit1sMemory limit128 MB
Count how many times a faster cow overtakes a slower cow, counting ordered pairs, before the fastest cow finishes L laps on a track of length C.
- Level
Medium7 of 10
- Topics
- Sorting, Math, Binary search, Array
- Solved
- No attempts yet
Problem
Farmer John decides to investigate whether cow racing could work as a sport. He lines up his cows () to run a race of laps around a circular track of length . All cows start at the same point and each runs at its own constant speed. The race ends the moment the fastest cow has covered the full distance .
During the race one cow may pass another. A crossing event is defined by an ordered pair of cows and a time (no later than the moment the race ends) at which cow moves ahead of cow . Count the total number of crossing events that occur during the entire race.
Input
- Line 1: three space-separated integers , , and ().
- Lines 2 to : line contains the speed of cow , an integer between and .
Output
- A single line with the total number of crossing events during the entire race.
Explanation
Consider 4 cows running 2 laps on a track of length 100 with speeds 20, 100, 70, and 1. The race lasts until the fastest cow (speed 100) finishes its 2 laps. During that time there are 4 crossing events: the speed-100 cow passes the speed-20 and speed-1 cows, and the speed-70 cow also passes the speed-20 and speed-1 cows.