CF612B.HDD is Outdated Technology
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.
One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.
Find the time need to read file split to n fragments. The i-th sector contains the f__i-th fragment of the file (1 ≤ f__i ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.
It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.
硬盘(HDD)将数据按扇区进行分组。所有文件都被分割为若干片段,每个片段被写入硬盘的某个扇区中。注意:这些片段可以以任意顺序写入扇区。
硬盘的一个问题是:磁头需要从一个扇区移动到另一个扇区才能读取某个文件。
现给定一个被分割为 n 个片段的文件,求读取该文件所需的时间。第 i 个扇区中存有该文件的第 fi 个片段(1≤fi≤n)。注意:不同扇区中存放的是不同的片段。初始时,磁头位于存放第一个片段的扇区。文件按如下方式读取:首先读取第一个片段;然后磁头移动至存放第二个片段的扇区,再读取第二个片段;依此类推,直至读取第 n 个片段。片段按从第 1 个到第 n 个的顺序依次读取。
磁头从扇区 a 移动到扇区 b 所需时间为 ∣a−b∣ 个时间单位。读取一个片段本身不消耗时间。
输入格式
The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments.
The second line contains n different integers f__i (1 ≤ f__i ≤ n) — the number of the fragment written in the i-th sector.
第一行包含一个正整数 n(1 ≤ n ≤ 2⋅105)——碎片的数量。
第二行包含 n 个互不相同的整数 fi(1 ≤ fi ≤ n)——第 i 个扇区上所写的碎片编号。
输出格式
Print the only integer — the number of time units needed to read the file.
输出唯一的整数——读取该文件所需的时间单位数。
输入输出样例
输入#1
3 3 1 2
输出#1
3
输入#2
5 1 3 5 4 2
输出#2
10
说明/提示
In the second example the head moves in the following way:
- 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units
- 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units
- 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units
- 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units
So the answer to the second example is 4 + 3 + 2 + 1 = 10.
在第二个例子中,磁头按以下方式移动:
- 1→2 表示从扇区 1 移动到扇区 5,即耗时 4 个时间单位
- 2→3 表示从扇区 5 移动到扇区 2,即耗时 3 个时间单位
- 3→4 表示从扇区 2 移动到扇区 4,即耗时 2 个时间单位
- 4→5 表示从扇区 4 移动到扇区 3,即耗时 1 个时间单位
因此,第二个例子的答案为 4+3+2+1=10。
输入解题思路,AI测评打分。不知道怎么写?