A9547 | Ryouko's Memory Note
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line of input contains two integers $n$ and $m (1<=n,m<=10^{5})$ .
The next line contains $m$ integers separated by spaces: $a_{1},a_{2},...,a_{m}$ $(1<=a_{i}<=n)$ .
The next line contains $m$ integers separated by spaces: $a_{1},a_{2},...,a_{m}$ $(1<=a_{i}<=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.
In the second sample, optimal solution is achieved by merging page 9 to 4.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted