Xenia and Ringroad
Description
Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.
Xenia has recently moved into the ringroad house number 1. As a result, she’s got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.
Xenia生活在一个城市,这个城市的主环路沿线建有n栋房子。环路上的房子按顺时针的顺序编号为1到n。环路的交通是单向的,也是顺时针方向的。
Xenia最近搬进了1号环路房屋。因此,她有m件事情要做。为了完成第i个任务,她需要在编号为ai的房子里,并完成所有数字小于i的任务。最初,Xenia在编号为1的房子里,如果从一个房子搬到环路沿线的相邻房子需要一个单位的时间,求她完成所有任务所需的最少时间。
Input
The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, …, am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.
第一行包含两个整数 n 和 m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105)。第二行包含m个整数a1, a2, …, am (1 ≤ ai ≤ n)。注意,Xenia可以在一个房子里有多个连续的任务。
Output
Print a single integer — the time Xenia needs to complete all tasks.
打印一个整数 - Xenia 完成所有任务所需的时间。
Input1
4 3
3 2 3
Output1
6
Input2
4 3
2 3 3
Output2
2
Note
In the first test example the sequence of Xenia’s moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
在第一个测试例子中,Xenia沿着环路的移动顺序如下。1 → 2 → 3 → 4 → 1 → 2 → 3. 这是最佳序列。所以,她需要6个时间单位。
"""解题说明:此题是一个n节点首尾相连的环状图,要求依次到达指定的节点,只能顺时针行走。只需要判断下一个节点与当前节点前后的关系,如果在前需要绕一圈才行。"""
def solve():
n, m = map(int, input().split(' ')) # n栋房子,m件事情
list_a = list(map(int, input().split())) # 创建list
count = list_a[0] - 1
for i in range(1, m):
if list_a[i-1] > list_a[i]:
count += n - (list_a[i-1] - list_a[i])
else:
count += list_a[i] - list_a[i-1]
print(count)
return 0