CF1066F.Yet another 2D Walking
提高+/省选-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Maksim walks on a Cartesian plane. Initially, he stands at the point (0,0) and in one move he can go to any of four adjacent points (left, right, up, down). For example, if Maksim is currently at the point (0,0), he can go to any of the following points in one move:
- (1,0);
- (0,1);
- (−1,0);
- (0,−1).
There are also n distinct key points at this plane. The i-th point is pi=(xi,yi). It is guaranteed that 0≤xi and 0≤yi and there is no key point (0,0).
Let the first level points be such points that max(xi,yi)=1, the second level points be such points that max(xi,yi)=2 and so on. Maksim wants to visit all the key points. But he shouldn't visit points of level i+1 if he does not visit all the points of level i. He starts visiting the points from the minimum level of point from the given set.
The distance between two points (x1,y1) and (x2,y2) is ∣x1−x2∣+∣y1−y2∣ where ∣v∣ is the absolute value of v.
Maksim wants to visit all the key points in such a way that the total distance he walks will be minimum possible. Your task is to find this distance.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
马克西姆在笛卡尔平面上行走。初始时,他位于点 (0,0),每次移动可前往四个相邻点之一(左、右、上、下)。例如,若马克西姆当前位于点 (0,0),则他可在一次移动中到达以下任意一点:
- (1,0);
- (0,1);
- (−1,0);
- (0,−1)。
平面上还有 n 个互异的关键点。第 i 个点为 pi=(xi,yi)。保证对所有 i 有 0≤xi 且 0≤yi,且不存在关键点 (0,0)。
定义“第一层”点为满足 max(xi,yi)=1 的点,“第二层”点为满足 max(xi,yi)=2 的点,依此类推。马克西姆需访问所有关键点,但在未访问完第 i 层的所有点之前,不得访问第 i+1 层的任何点。他从给定关键点集合中最小的层数开始访问。
两点 (x1,y1) 与 (x2,y2) 之间的距离定义为 ∣x1−x2∣+∣y1−y2∣,其中 ∣v∣ 表示 v 的绝对值。
马克西姆希望以总行走距离最小的方式访问全部关键点。你的任务是求出该最小总距离。
如果你使用 Python 编程,请在提交代码时考虑使用 PyPy 而非 Python。
输入格式
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of key points.
Each of the next n lines contains two integers xi, yi (0≤xi,yi≤109) — x-coordinate of the key point pi and y-coordinate of the key point pi. It is guaranteed that all the points are distinct and the point (0,0) is not in this set.
输入的第一行包含一个整数 n(1≤n≤2⋅105)—— 关键点的数量。
接下来的 n 行中,每行包含两个整数 xi、yi(0≤xi,yi≤109)—— 关键点 pi 的 x 坐标和 y 坐标。保证所有点互不相同,且点 (0,0) 不在该集合中。
输出格式
Print one integer — the minimum possible total distance Maksim has to travel if he needs to visit all key points in a way described above.
输出一个整数——即马克西姆按上述方式访问所有关键点所需经过的最小总距离。
输入输出样例
输入#1
8 2 2 1 4 2 3 3 1 3 4 1 1 4 3 1 2
输出#1
15
输入#2
5 2 1 1 0 2 0 3 2 0 3
输出#2
9
说明/提示
The picture corresponding to the first example: 
There is one of the possible answers of length 15.
The picture corresponding to the second example: 
There is one of the possible answers of length 9.
第一个示例对应的图片:
存在一个长度为 15 的可能答案。
第二个示例对应的图片:
存在一个长度为 9 的可能答案。
输入解题思路,AI测评打分。不知道怎么写?