CF1922C.Closest Cities
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There are n cities located on the number line, the i -th city is in the point ai . The coordinates of the cities are given in ascending order, so a1<a2<⋯<an .
The distance between two cities x and y is equal to ∣ax−ay∣ .
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 ∣ax−ay∣ 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≤t≤104 ) — the number of test cases.
Each test case is given in the following format:
- the first line contains one integer n ( 2≤n≤105 );
- the second line contains n integers a1,a2,…,an ( 0≤a1<a2<⋯<an≤109 );
- the third line contains one integer m ( 1≤m≤105 );
- then m lines follow; the i -th of them contains two integers xi and yi ( 1≤xi,yi≤n ; xi=yi ), 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 xi to the city yi .
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 105 ;
- the sum of m over all test cases does not exceed 105 .
输出格式
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 .