CF339B.Xenia and Ringroad

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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 a__i 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 居住在一座拥有 nn 座房屋的城市中,这些房屋沿主环形道路依次建造。环形道路上的房屋按顺时针方向编号为 11nn。环形道路为单向通行,且行驶方向为顺时针。

Xenia 最近搬入了环形道路上编号为 11 的房屋。因此,她共有 mm 项事务需要处理。为完成第 ii 项事务,她必须身处编号为 aia_i 的房屋,并且需先完成所有编号小于 ii 的事务。初始时,Xenia 位于编号为 11 的房屋。假设沿环形道路从一座房屋移动到相邻房屋耗时一个单位时间,求她完成全部事务所需的最少时间。

输入格式

The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers _a_1, _a_2, ..., a__m (1 ≤ a__i ≤ n). Note that Xenia can have multiple consecutive tasks in one house.

第一行包含两个整数 nnmm2n1052 \leq n \leq 10^51m1051 \leq m \leq 10^5)。第二行包含 mm 个整数 a1,a2,,ama_1,\,a_2,\,\dots,\,a_m1ain1 \leq a_i \leq n)。注意,Xenia 可以在同一个房屋中连续完成多个任务。

输出格式

Print a single integer — the time Xenia needs to complete all tasks.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

输出一个整数——Xenia 完成所有任务所需的时间。

请注意,在 C++ 中不要使用 %lld 说明符来读取或写入 64 位整数。推荐使用 cincout 流,或 %I64d 说明符。

输入输出样例

  • 输入#1

    4 3
    3 2 3

    输出#1

    6
  • 输入#2

    4 3
    2 3 3

    输出#2

    2

说明/提示

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 个时间单位。

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

首页