luogu#P2963. [USACO09NOV] Cow Rescue G

[USACO09NOV] Cow Rescue G

题目描述

Bessie is trapped in a triangular maze with N rows (1 <= N <= 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 <= M <= 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.

贝希被困在一个三角形的迷宫之中。这个迷宫有 NN 行(1N10000001 \le N \le 1000000)。比如下图是一个 33 行的迷宫。 迷宫的第 ii 行有 2i12i-1 个三角形,从左到右分别编号为 (i,1)(i, 1)(i,2)(i, 2) 等等。

贝希每次可以从一个三角形走到任意一个一个跟当前的三角形有邻边的三角形。

比如说,如果她目前处于三角形 (3,3)(3, 3),那么,她可以走到三角形 (3,2)(3, 2)(3,4)(3, 4)(4,4)(4, 4)。贝希每次需要一分钟的时间来移动到下一个三角形。

农夫约翰发现贝希被困了!于是她跟踪贝希的iPhone手机(可怜的触摸屏~),得知贝希目前处于三角形 (Si,Sj)(S_i, S_j)

因为约翰对贝希有著无穷无尽的浓浓爱意,所以他希望贝希能尽可能快地回到他的身边。 在迷宫的三角形之中,有 MM1M100001 \le M \le 10000)个是出口。在任何一个出口都可以让贝希逃离迷宫。一旦贝希进入一个作为出口的三角形,她用多一分钟就可以逃离这个迷宫。 找到一个可以让贝希逃离迷宫最小时间 TT,并输出她应该从哪一个出口逃离迷宫,这个出口记为 (OUTi,OUTj)(\text{OUT}_i, \text{OUT}_j)

如果有多个出口同时需要时间 TT,输出那个行的编号小的出口,如果仍然有多个出口,输出那个列的编号小的。

输入格式

* 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

输出格式

* Line 1: Two space-separated integers: OUTi and OUTj

* Line 2: A single integer: T

4 2 
2 1 
3 5 
4 4 

4 4 
4