SCC25 Contest 1 P2 - Haunted Hallway
Dr. Kurt, the mad scientist, has gathered \(N\) ghosts in an infinitely long haunted hallway in order to scare the kids in his SCH4U class. Each ghost has a starting position \(x_{i}\) and a drifting speed of \(v_{i}\). At the start of class, each ghost will drift rightward. If two ghosts collide, they merge into a spookier ghost and continue moving rightward at the speed of the slower of the two original ghosts. This process of merging can repeat indefinitely. He challenges the kids in his class to predict the total number of merges between the ghosts while murmuring something about collision theory. Rohan, who desperately needs the extra credit, asks you for help in order to solve this eerie exercise.
Note: If multiple collisions happen at the same time, each one is counted individually.
Constraints
All \(x_{i}\) values are distinct and will be given in ascending order.
\(0 \le x_{i}, v_{i} \le 10^{9}\)
Subtask 1 [30%]
\(1 \le N \le 10^{3}\)
Subtask 2 [70%]
\(1 \le N \le 10^{6}\)
Input Specification
The first line contains \(N\).
The next \(N\) lines each contain two integers, \(x_{i}, v_{i}\).
Output Specification
Output the total number of merges that occur.
Sample Input 1
3
1 3
2 2
3 1
Sample Output 1
2
Sample Input 1
3
1 2
2 1
3 3
Sample Output 1
1
Comments