A12259 | Yet another 2D Walking
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $p_i = (x_i, y_i)$ . It is guaranteed that $0 \le x_i$ and $0 \le y_i$ and there is no key point $(0, 0)$ .
Let the first level points be such points that $max(x_i, y_i) = 1$ , the second level points be such points that $max(x_i, y_i) = 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 $(x_1, y_1)$ and $(x_2, y_2)$ is $|x_1 - x_2| + |y_1 - y_2|$ 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.
- $(1, 0)$ ;
- $(0, 1)$ ;
- $(-1, 0)$ ;
- $(0, -1)$ .
There are also $n$ distinct key points at this plane. The $i$ -th point is $p_i = (x_i, y_i)$ . It is guaranteed that $0 \le x_i$ and $0 \le y_i$ and there is no key point $(0, 0)$ .
Let the first level points be such points that $max(x_i, y_i) = 1$ , the second level points be such points that $max(x_i, y_i) = 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 $(x_1, y_1)$ and $(x_2, y_2)$ is $|x_1 - x_2| + |y_1 - y_2|$ 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.
输入格式
The first line of the input contains one integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of key points.
Each of the next $n$ lines contains two integers $x_i$ , $y_i$ ( $0 \le x_i, y_i \le 10^9$ ) — $x$ -coordinate of the key point $p_i$ and $y$ -coordinate of the key point $p_i$ . It is guaranteed that all the points are distinct and the point $(0, 0)$ is not in this set.
Each of the next $n$ lines contains two integers $x_i$ , $y_i$ ( $0 \le x_i, y_i \le 10^9$ ) — $x$ -coordinate of the key point $p_i$ and $y$ -coordinate of the key point $p_i$ . It is guaranteed that all the points are distinct and the point $(0, 0)$ is not in this set.
输出格式
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted