CF534E.Berland Local Positioning System
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are n bus stops located along the street, the i-th of them is located at the distance a__i from the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, a__i < a__i + 1 for all i from 1 to n - 1. The bus starts its journey from the first stop, it passes stops 2, 3 and so on. It reaches the stop number n, turns around and goes in the opposite direction to stop 1, passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop n. During the day, the bus runs non-stop on this route.
The bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number.
One of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order.
For example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5, ends at the stop number 3 and passes the stops as follows:
, then the request about this segment of the path will have form: 3, 4, 5, 5, 6. If the bus on the segment of the path from stop 5 to stop 3 has time to drive past the 1-th stop (i.e., if we consider a segment that ends with the second visit to stop 3 on the way from 5), then the request will have form: 1, 2, 2, 3, 3, 4, 5, 5, 6.
You will have to repeat the Berland programmers achievement and implement this function.
在贝尔兰,一辆公交车沿首都的主干道行驶。该街道从中心广场开始,呈一条极长的线段状。街道沿线设有 n 个公交站,其中第 i 个站距中心广场的距离为 ai;所有距离互不相同,且各站按距广场距离递增顺序编号,即对所有 i=1,2,…,n−1,均有 ai<ai+1。公交车从第 1 站出发,依次经过第 2、3、… 站,直至到达第 n 站;随后掉头,沿反方向驶向第 1 站,并以相反顺序途经所有中间站点;之后再次调头驶向第 n 站。如此往复,公交车全天不间断地运行于该环形路线上。
公交车配备了贝尔兰本地定位系统。每当公交车经过某一站时,系统即记录该站编号。
该系统的一项关键功能是响应关于路径上某两站之间路段所行驶距离的查询。系统中一个专用模块接收输入:一段路径上所经过站点的集合(某站编号在集合中出现的次数,等于公交车在该路径段中经过该站的次数)。该模块返回该路径段的总长度(若无法唯一确定长度,则返回 −1)。该模块运作的难点在于:输入中站点编号并非按实际经过顺序给出,而是按非递减顺序排列。
例如,若共有 6 个站点,而某段公交路径起始于第 5 站、终止于第 3 站,且途经站点顺序如下:

则该路径段对应的查询输入形式为:3, 4, 5, 5, 6。
若在从第 5 站到第 3 站的该路径段中,公交车还来得及经过第 1 站(即考虑的是从第 5 站出发、最终第二次抵达第 3 站的那段路径),则查询输入形式为:1, 2, 2, 3, 3, 4, 5, 5, 6。
你需要重现贝尔兰程序员的成果,实现该函数。
输入格式
The first line contains integer n (2 ≤ n ≤ 2·105) — the number of stops.
The second line contains n integers (1 ≤ a__i ≤ 109) — the distance from the i-th stop to the central square. The numbers in the second line go in the increasing order.
The third line contains integer m (1 ≤ m ≤ 4·105) — the number of stops the bus visited on some segment of the path.
The fourth line contains m integers (1 ≤ b__i ≤ n) — the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus.
It is guaranteed that the query corresponds to some segment of the path.
第一行包含一个整数 n(2≤n≤2⋅105)—— 表示公交站的数量。
第二行包含 n 个整数(1≤ai≤109)—— 表示第 i 个公交站到中心广场的距离。第二行中的数字按升序排列。
第三行包含一个整数 m(1≤m≤4⋅105)—— 表示公交车在某一段路径上所访问的公交站数量。
第四行包含 m 个整数(1≤bi≤n)—— 表示公交车在该段路径上所访问的公交站编号的有序列表。某个公交站的编号出现多少次,表示公交车访问该站多少次。
保证该查询对应于路径上的某一段连续子路径。
输出格式
In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print - 1.
请在单行中输出公交车行驶的距离。如果无法明确确定该距离,请输出 -1。
输入输出样例
输入#1
6 2 3 5 7 11 13 5 3 4 5 5 6
输出#1
10
输入#2
6 2 3 5 7 11 13 9 1 2 2 3 3 4 5 5 6
输出#2
16
输入#3
3 10 200 300 4 1 2 2 3
输出#3
-1
输入#4
3 1 2 3 4 1 2 2 3
输出#4
3
说明/提示
The first test from the statement demonstrates the first example shown in the statement of the problem.
The second test from the statement demonstrates the second example shown in the statement of the problem.
In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely.
In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely.
题目描述中的第一个测试用例展示了问题陈述中给出的第一个示例。
题目描述中的第二个测试用例展示了问题陈述中给出的第二个示例。
在第三个样例中,存在两条长度不同的可能路径,因此所求线段的长度无法唯一确定。
在第四个样例中,尽管查询对应两条不同的路径,但它们的长度相同,因此所求线段的长度可以唯一确定。
输入解题思路,AI测评打分。不知道怎么写?