QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#1914. Falling Portals

統計

There are $N$ ($2\le N\le 2\cdot 10^5$) worlds, each with a portal. Initially, world $i$ (for $1 \leq i \leq N$) is at $x$-coordinate $i$, and $y$-coordinate $A_i$ ($1\le A_i\le 10^9$). There is also a cow on each world. At time $0$, all $y$-coordinates are distinct and the worlds start falling: world $i$ moves continuously in the negative-$y$ direction at a speed of $i$ units per second.

At any time when two worlds are at the same $y$-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 $i$, the cow on world $i$ wants to travel to world $Q_i$ ($Q_i\neq i$). Help each cow determine how long her journey will take, if she travels optimally.

Each query output should be a fraction $a/b$ where $a$ and $b$ are positive and relatively prime integers, or $-1$ if it the journey is impossible.

Scoring

  • Test cases 2-3 satisfy $N\le 100$.
  • Test cases 4-5 satisfy $N\le 2000$.
  • Test cases 6-14 satisfy no additional constraints.

Input Format

The first line of input contains a single integer $N.$

The next line contains $N$ space-separated integers $A_1,A_2,\ldots,A_N$.

The next line contains $N$ space-separated integers $Q_1,Q_2,\ldots,Q_N$.

Output Format

Print $N$ lines, the $i$-th of which contains the journey length for cow $i.$

Sample Input

4
3 5 10 2
3 3 2 1

Sample Output

7/2
7/2
5/1
-1

Consider the answer for the cow originally on world 2. At time $2$ worlds 1 and 2 align, so the cow can travel to world 1. At time $\frac{7}{2}$ worlds 1 and 3 align, so the cow can travel to world 3.

Problem credits: Dhruv Rohatgi

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.