Falling Portals
Time limit2sMemory limit512 MB
Each world falls at speed i, and a cow can jump to another world when their portals align. Find each cow's earliest arrival time at its target world.
- Level
Hard8 of 10
- Topics
- Simulation, Sorting, Union-find
- Solved
- No attempts yet
Problem
There are () worlds, each with a portal. Initially, world (for ) is at -coordinate , and -coordinate (). There is also a cow on each world. At time , all -coordinates are distinct and the worlds start falling: world moves continuously in the negative- direction at a speed of units per second.
At any time when two worlds are at the same -coordinate (possibly a fractional time), the portals align, meaning that a cow on one of the worlds can choose to travel instantaneously to the other world.
For each , the cow on world wants to travel to world (). Determine how long her journey will take if she travels optimally.
Each answer is a fraction where and are positive, relatively prime integers, or if the journey is impossible.
Input
The first line contains a single integer .
The next line contains space-separated integers .
The next line contains space-separated integers .
Output
Print lines. The -th line contains the journey length for cow .
Hint
Consider the answer for the cow originally on world 2. At time worlds 1 and 2 align, so the cow can travel to world 1. At time worlds 1 and 3 align, so the cow can travel to world 3.