The Race
Time limit1sMemory limit128 MB
Count all overtakes among spaceships with given starting positions and speeds, then list the first 10000 in time order.
- Level
Hard8 of 10
- Topics
- Sorting, Greedy, Implementation, Geometry
- Solved
- No attempts yet
Problem
During the annual interstellar competition for tuned spaceships, spaceships compete. Each spaceship is tuned so that it can accelerate in zero time to its maximum speed and then cruise at that speed forever. Because of past achievements, each spaceship starts at position , i.e. it begins kilometers past the starting line.
The race course is infinitely long. Because the spaceships move so fast, the course runs perfectly straight the whole way. On this straight course, spaceships can pass one another very easily, without interfering with each other.
Many spectators have not yet realized that the outcome of the race can be determined in advance. Your task is to show this: report how many times spaceships pass one another, and predict the first passings in chronological order.
You may assume that every spaceship starts at a different position. Furthermore, no more than two spaceships are ever at the same position on the course at the same time.
Input
The input consists of several races. The first line of each race contains the number of spaceships (). Each of the next lines describes one spaceship: the -th line contains two integers and , the starting position and the velocity of the -th spaceship (, ). The spaceships are ordered by starting position, i.e. . The starting position is the number of kilometers past the starting line where the spaceship starts, and the velocity is given in kilometers per second.
The input is terminated by a race with spaceships; no output should be produced for that race.
Output
For each race, output the following. The first line contains the number of times spaceships pass one another during the race, taken modulo .
Each of the following lines represents one passing, in chronological order. If there are more than passings, output only the first ; if there are fewer than , output all of them. Each line consists of two integers and , meaning that spaceship passes spaceship . If several passings happen at the same time, sort them by their position on the course: a passing that takes place closer to the starting line is listed first. The time of a passing is the moment when the two spaceships are at the same position.