A16367 | Closest Cities
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There are $n$ cities located on the number line, the $i$ -th city is in the point $a_i$ . The coordinates of the cities are given in ascending order, so $a_1 < a_2 < \dots < a_n$ .
The distance between two cities $x$ and $y$ is equal to $|a_x - a_y|$ .
For each city $i$ , let's define the closest city $j$ as the city such that the distance between $i$ and $j$ is not greater than the distance between $i$ and each other city $k$ . For example, if the cities are located in points $[0, 8, 12, 15, 20]$ , then:
- the closest city to the city $1$ is the city $2$ ;
- the closest city to the city $2$ is the city $3$ ;
- the closest city to the city $3$ is the city $4$ ;
- the closest city to the city $4$ is the city $3$ ;
- the closest city to the city $5$ is the city $4$ .
The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points $[1, 2, 3]$ , since this would mean that the city $2$ has two closest cities ( $1$ and $3$ , both having distance $1$ ).
You can travel between cities. Suppose you are currently in the city $x$ . Then you can perform one of the following actions:
- travel to any other city $y$ , paying $|a_x - a_y|$ coins;
- travel to the city which is the closest to $x$ , paying $1$ coin.
You are given $m$ queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.
The distance between two cities $x$ and $y$ is equal to $|a_x - a_y|$ .
For each city $i$ , let's define the closest city $j$ as the city such that the distance between $i$ and $j$ is not greater than the distance between $i$ and each other city $k$ . For example, if the cities are located in points $[0, 8, 12, 15, 20]$ , then:
- the closest city to the city $1$ is the city $2$ ;
- the closest city to the city $2$ is the city $3$ ;
- the closest city to the city $3$ is the city $4$ ;
- the closest city to the city $4$ is the city $3$ ;
- the closest city to the city $5$ is the city $4$ .
The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points $[1, 2, 3]$ , since this would mean that the city $2$ has two closest cities ( $1$ and $3$ , both having distance $1$ ).
You can travel between cities. Suppose you are currently in the city $x$ . Then you can perform one of the following actions:
- travel to any other city $y$ , paying $|a_x - a_y|$ coins;
- travel to the city which is the closest to $x$ , paying $1$ coin.
You are given $m$ queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.
输入格式
The first line contains one integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases.
Each test case is given in the following format:
- the first line contains one integer $n$ ( $2 \le n \le 10^5$ );
- the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_1 < a_2 < \dots < a_n \le 10^9$ );
- the third line contains one integer $m$ ( $1 \le m \le 10^5$ );
- then $m$ lines follow; the $i$ -th of them contains two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ ; $x_i \ne y_i$ ), denoting that in the $i$ -th query, you have to calculate the minimum number of coins you have to spend to travel from the city $x_i$ to the city $y_i$ .
Additional constraints on the input:
- in every test case, for each city, the closest city is determined uniquely;
- the sum of $n$ over all test cases does not exceed $10^5$ ;
- the sum of $m$ over all test cases does not exceed $10^5$ .
Each test case is given in the following format:
- the first line contains one integer $n$ ( $2 \le n \le 10^5$ );
- the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \le a_1 < a_2 < \dots < a_n \le 10^9$ );
- the third line contains one integer $m$ ( $1 \le m \le 10^5$ );
- then $m$ lines follow; the $i$ -th of them contains two integers $x_i$ and $y_i$ ( $1 \le x_i, y_i \le n$ ; $x_i \ne y_i$ ), denoting that in the $i$ -th query, you have to calculate the minimum number of coins you have to spend to travel from the city $x_i$ to the city $y_i$ .
Additional constraints on the input:
- in every test case, for each city, the closest city is determined uniquely;
- the sum of $n$ over all test cases does not exceed $10^5$ ;
- the sum of $m$ over all test cases does not exceed $10^5$ .
输出格式
For each query, print one integer — the minimum number of coins you have to spend.
输入输出样例
输入 #1
1 5 0 8 12 15 20 5 1 4 1 5 3 4 3 2 5 1
输出 #1
3 8 1 4 14
Let's consider the first two queries in the example from the statement:
- in the first query, you are initially in the city $1$ . You can travel to the closest city (which is the city $2$ ), paying $1$ coin. Then you travel to the closest city (which is the city $3$ ) again, paying $1$ coin. Then you travel to the closest city (which is the city $4$ ) again, paying $1$ coin. In total, you spend $3$ coins to get from the city $1$ to the city $4$ ;
- in the second query, you can use the same way to get from the city $1$ to the city $4$ , and then spend $5$ coins to travel from the city $4$ to the city $5$ .
- in the first query, you are initially in the city $1$ . You can travel to the closest city (which is the city $2$ ), paying $1$ coin. Then you travel to the closest city (which is the city $3$ ) again, paying $1$ coin. Then you travel to the closest city (which is the city $4$ ) again, paying $1$ coin. In total, you spend $3$ coins to get from the city $1$ to the city $4$ ;
- in the second query, you can use the same way to get from the city $1$ to the city $4$ , and then spend $5$ coins to travel from the city $4$ to the city $5$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted