QOJ.ac

QOJ

时间限制: 2 s 内存限制: 256 MB 总分: 100

#22426. Cow Rescue

统计

Bessie is trapped in a triangular maze with $N$ rows ($1 \le N \le 1,000,000$). A three row maze is shown below:

The $i$'th row of the maze contains $2*i-1$ triangles. Numbering from the left, the triangles are named ($i$,1), ($i$,2), and so on.

Bessie can travel to the (often three) triangles which share an edge with her current triangle. For example, if she is at (3, 3), she can travel to (3, 2), (3, 4) and (4, 4). Bessie takes one minute to travel from one triangle to the next.

FJ has learned the Bessie is trapped and knows by tracking her iPhone that she starts her exit trek at triangle ($Si,Sj$). FJ's love for Bessie knows no bounds so he wants her back in the minimum possible time.

The maze has $M$ ($1 \le M \le 10,000$) exits found in locations throughout the set of triangles. Any one of these will enable Bessie to escape. Once she enters an exit triangle, she leaves the maze in just one more minute.

Find the minimum time in minutes, $T$, required for Bessie to exit the maze and report the optimal exit location she uses, ($OUTi$, $OUTj$). If more than one location requires only $T$ minutes, output the location with the smallest row. If two optimal rows are the same, output the one with smaller column.

Input

  • Line 1: Two space-separated integers: $N$ and $M$
  • Line 2: Two space-separated integers: $Si$ and $Sj$
  • Lines $3..M+2$: Line $i+2$ contains two space-separated integers that are the triangle location of exit $i$: $Ei$ and $Ej$

Output

  • Line 1: Two space-separated integers: $OUTi$ and $OUTj$
  • Line 2: A single integer: $T$

Examples

Input 1

4 2
2 1
3 5
4 4

Output 1

4 4
4

Problem credits: Ye Wang, 2009

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.