CF433C.Ryouko's Memory Note

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.

Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.

Ryouko's notebook consists of n pages, numbered from 1 to n. To make life (and this problem) easier, we consider that to turn from page x to page y, |x - y| pages should be turned. During analyzing, Ryouko needs m pieces of information, the i-th piece of information is on page a__i. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is .

Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page x to page y, she would copy all the information on page x to y (1 ≤ x, y ≤ n), and consequently, all elements in sequence a that was x would become y. Note that x can be equal to y, in which case no changes take place.

Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.

凉子是一个极其健忘的女孩,甚至会忘记刚刚发生的事情。因此,为了记住事情,她随身携带一本名为“凉子的记忆笔记”的笔记本。她将所见所闻记录在这本笔记本上,而这本笔记本便成了她的记忆。

虽然凉子健忘,但她天生拥有卓越的分析能力。然而,分析工作在很大程度上依赖于所收集的信息,即记忆。因此,每当她需要进行分析时,都不得不翻阅自己的笔记本,这是一项繁重的工作。

凉子的笔记本共有 $ n $ 页,页码从 $ 1 $ 到 $ n $ 编号。为使生活(以及本题)更简单,我们定义:从第 $ x $ 页翻到第 $ y $ 页,需翻动 $ |x - y| $ 页。在分析过程中,凉子需要 $ m $ 条信息,其中第 $ i $ 条信息位于第 $ a_i $ 页。这些信息必须按顺序从笔记本中读取,因此凉子总共需要翻动的页数为
。

凉子希望减少所需翻动的页数。为实现这一目标,她可以将笔记本中的两页进行合并。若凉子将第 $ x $ 页合并至第 $ y $ 页($ 1 \le x, y \le n $),她会将第 $ x $ 页上的所有信息全部复制到第 $ y $ 页上;相应地,序列 $ a $ 中所有原本为 $ x $ 的元素都将变为 $ y 。注意,。注意, x $ 可以等于 $ y $,此时不发生任何变化。

请告诉凉子:在最多执行一次上述操作的前提下,她最少需要翻动多少页?注意,答案可能超过 32 位整数范围。

输入格式

The first line of input contains two integers n and m (1 ≤ n, m ≤ 105).

The next line contains m integers separated by spaces: _a_1, _a_2, ..., a__m (1 ≤ a__i ≤ n).

输入的第一行包含两个整数 nn 和 mm(1 ≤ n, m ≤ 1051 \leq n, m \leq 10^5)。

下一行包含 mm 个用空格分隔的整数:a1, a2, ..., ama_1,\,a_2,\,...,\,a_m(1 ≤ ai ≤ n1 \leq a_i \leq n)。

输出格式

Print a single integer — the minimum number of pages Ryouko needs to turn.

输出一个整数——凉子需要翻动的最少页数。

输入输出样例

  • 输入#1

    4 6
    1 2 3 4 3 2

    输出#1

    3
  • 输入#2

    10 5
    9 4 3 8 8

    输出#2

    6

说明/提示

In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence a becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.

In the second sample, optimal solution is achieved by merging page 9 to 4.

在第一个样例中,最优解是将第 4 页合并到第 3 页,合并后序列 aa 变为 {1, 2, 3, 3, 3, 2}\{1,\,2,\,3,\,3,\,3,\,2\},因此凉子需要翻动的页数为 ∣1 − 2∣ + ∣2 − 3∣ + ∣3 − 3∣ + ∣3 − 3∣ + ∣3 − 2∣ = 3|1\,-\,2|\,+\,|2\,-\,3|\,+\,|3\,-\,3|\,+\,|3\,-\,3|\,+\,|3\,-\,2|\,=\,3。

在第二个样例中,通过将第 9 页合并到第 4 页可达到最优解。

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

首页