CF1922C.Closest Cities

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are nn cities located on the number line, the ii-th city is in the point aia_i. The coordinates of the cities are given in ascending order, so a1<a2<⋯<ana_1 \lt a_2 \lt \dots \lt a_n.

The distance between two cities xx and yy is equal to ∣ax−ay∣|a_x - a_y|.

For each city ii, let's define the closest city jj as the city such that the distance between ii and jj is not greater than the distance between ii and each other city kk. For example, if the cities are located in points [0,8,12,15,20][0, 8, 12, 15, 20], then:

  • the closest city to the city 11 is the city 22;
  • the closest city to the city 22 is the city 33;
  • the closest city to the city 33 is the city 44;
  • the closest city to the city 44 is the city 33;
  • the closest city to the city 55 is the city 44.

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][1, 2, 3], since this would mean that the city 22 has two closest cities (11 and 33, both having distance 11).

You can travel between cities. Suppose you are currently in the city xx. Then you can perform one of the following actions:

  • travel to any other city yy, paying ∣ax−ay∣|a_x - a_y| coins;
  • travel to the city which is the closest to xx, paying 11 coin.

You are given mm 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.

数轴上有 nn 座城市,第 ii 座城市位于坐标 aia_i 处。城市的坐标按升序给出,即满足 a1<a2<⋯<ana_1 \lt a_2 \lt \dots \lt a_n。

两座城市 xx 与 yy 之间的距离定义为 ∣ax−ay∣|a_x - a_y|。

对每座城市 ii,我们定义其最近城市 jj 为满足以下条件的城市:城市 ii 到城市 jj 的距离不大于城市 ii 到任意其他城市 kk 的距离。例如,若城市坐标为 [0,8,12,15,20][0, 8, 12, 15, 20],则:

  • 城市 11 的最近城市是城市 22;
  • 城市 22 的最近城市是城市 33;
  • 城市 33 的最近城市是城市 44;
  • 城市 44 的最近城市是城市 33;
  • 城市 55 的最近城市是城市 44。

题目保证:对每座城市,其最近城市是唯一的。例如,城市坐标不可能为 [1,2,3][1, 2, 3],因为此时城市 22 将有两个最近城市(城市 11 和城市 33,距离均为 11)。

你可以在城市之间旅行。假设你当前位于城市 xx,则你可以执行以下任一操作:

  • 前往任意其他城市 yy,花费 ∣ax−ay∣|a_x - a_y| 枚金币;
  • 前往城市 xx 的最近城市,仅花费 11 枚金币。

现给出 mm 个查询。每个查询给出两个城市编号,你需要计算从一座城市到达另一座城市的最小金币花费。

输入格式

The first line contains one integer tt (1≤t≤1041 \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 nn (2≤n≤1052 \le n \le 10^5);
  • the second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤a1<a2<⋯<an≤1090 \le a_1 \lt a_2 \lt \dots \lt a_n \le 10^9);
  • the third line contains one integer mm (1≤m≤1051 \le m \le 10^5);
  • then mm lines follow; the ii-th of them contains two integers xix_i and yiy_i (1≤xi,yi≤n1 \le x_i, y_i \le n; xi≠yix_i \ne y_i), denoting that in the ii-th query, you have to calculate the minimum number of coins you have to spend to travel from the city xix_i to the city yiy_i.

Additional constraints on the input:

  • in every test case, for each city, the closest city is determined uniquely;
  • the sum of nn over all test cases does not exceed 10510^5;
  • the sum of mm over all test cases does not exceed 10510^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的格式如下:

  • 第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5);
  • 第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤a1<a2<⋯<an≤1090 \le a_1 \lt a_2 \lt \dots \lt a_n \le 10^9);
  • 第三行包含一个整数 mm(1≤m≤1051 \le m \le 10^5);
  • 接下来 mm 行,第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1 \le x_i, y_i \le n;xi≠yix_i \ne y_i),表示在第 ii 个查询中,你需要计算从城市 xix_i 到城市 yiy_i 所需花费的最少硬币数。

输入的额外约束条件:

  • 在每个测试用例中,对每个城市而言,其最近的城市是唯一确定的;
  • 所有测试用例的 nn 值之和不超过 10510^5;
  • 所有测试用例的 mm 值之和不超过 10510^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 11. You can travel to the closest city (which is the city 22), paying 11 coin. Then you travel to the closest city (which is the city 33) again, paying 11 coin. Then you travel to the closest city (which is the city 44) again, paying 11 coin. In total, you spend 33 coins to get from the city 11 to the city 44;
  • in the second query, you can use the same way to get from the city 11 to the city 44, and then spend 55 coins to travel from the city 44 to the city 55.

我们考虑题目陈述中的前两个查询:

  • 在第一个查询中,你初始位于城市 11。你可以前往最近的城市(即城市 22),花费 11 枚硬币;接着再次前往最近的城市(即城市 33),花费 11 枚硬币;然后再一次前往最近的城市(即城市 44),花费 11 枚硬币。总计花费 33 枚硬币,从城市 11 到达城市 44;
  • 在第二个查询中,你可以采用相同的方式从城市 11 到达城市 44,然后额外花费 55 枚硬币从城市 44 前往城市 55。

输入解题思路,AI测评打分。不知道怎么写?

首页