C. Ryouko's Memory Note
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ai.
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.

Input

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

The next line contains m integers separated by spaces: a1, a2, ..., am (1 ≤ ai ≤ n).

Output

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

Sample test(s)
input
4 6
1 2 3 4 3 2
output
3
input
10 5
9 4 3 8 8
output
6
Note

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.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=100100; typedef long long int LL; int n,m;
LL a[maxn];
vector<int> near[maxn]; int main()
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>a[i];
}
LL cur=0,orz;
for(int i=0;i<m-1;i++)
{
cur+=abs(a[i+1]-a[i]);
}
orz=cur;
for(int i=0;i<m;i++)
{
if(i-1>=0&&a[i-1]!=a[i])
{
near[a[i]].push_back(a[i-1]);
}
if(i+1<m&&a[i]!=a[i+1])
{
near[a[i]].push_back(a[i+1]);
}
}
for(int i=1;i<=n;i++)
{
int sz=near[i].size();
if(sz)
{
sort(near[i].begin(),near[i].end());
LL change=0;int mid=near[i][sz/2];
for(int j=0;j<sz;j++)
{
change+=abs(near[i][j]-mid)-abs(near[i][j]-i);
}
cur=min(cur,orz+change);
}
}
cout<<cur<<endl;
return 0;
}

Codeforces 433 C. Ryouko&#39;s Memory Note的更多相关文章

  1. Codeforces Round #248 (Div. 1)——Ryouko&#39;s Memory Note

    题目连接 题意: 给n和m,一行m个1<=x<=n的数.记c=.如今仅仅能选择一个数x变成y,序列中全部等于x的值都变成y,求最小的c 分析: 对于一个数x,把与他相邻的所有的非x的数所有 ...

  2. Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题

    A. Ryouko's Memory Note 题目连接: http://www.codeforces.com/contest/434/problem/A Description Ryouko is ...

  3. codeforces 434A A. Ryouko's Memory Note(数学)

    题目链接: A. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input ...

  4. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note

    题目链接:http://codeforces.com/contest/433/problem/C 思路:可以想到,要把某一个数字变成他的相邻中的数字的其中一个,这样总和才会减少,于是我们可以把每个数的 ...

  5. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

  6. Codeforces Round #248 (Div. 2) C. Ryouko's Memory Note (vector 替换)

    题目链接 题意:给m个数字, 这些数字都不大于 n,  sum的值为相邻两个数字 差的绝对值.求这n个数字里把一个数字 用 其中另一个数字代替以后, 最小的sum值. 分析:刚开始以为两个for 最坏 ...

  7. codeforces C. Ryouko's Memory Note

    题意:给你m个数,然后你选择一个数替换成别的数,使得.最小.注意选择的那个数在这m个数与它相同的数都必须替换同样的数. 思路:用vector记录每一个数与它相邻的数,如果相同不必记录,然后遍历替换成与 ...

  8. CodeForces 433C Ryouko's Memory Note (中位数定理)

    <题目链接> 题目大意:给你一堆数字,允许你修改所有相同的数字成为别的数字,不过只能修改一次,问你修改后序列相邻数字的距离和最小是多少. 解题分析: 首先,修改不是任意的,否则那样情况太多 ...

  9. Ryouko's Memory Note

    题目意思:一个书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么|x-y|页都需要翻到(联系生活实际就很容易理解的了).接着有m pieces 的 information ...

随机推荐

  1. FTA

    FTA - 维基百科,自由的百科全书 FTA 维基百科,自由的百科全书   跳转至: 导航. 搜索 FTA可以指: 自由贸易协定(Free Trade Agreement) 自由贸易区(Free Tr ...

  2. AIX5.3CPU占用高的问题核查

    AIX5.3 CPU占用高问题核查步骤 1.topas查看占用cpu占用最高的进程的PID 2.执行: ps -mp PID -o THREAD 以查找相应进程下所有正在占用 CPU 的线程的TID ...

  3. 遍历父视图上的button

    for (UIView * thebtn in [self.view subviews]) { if ([thebtn isKindOfClass:[UIButton class]]) { //*** ...

  4. 解读JavaScript代码 var ie = !-[1,]

    var ie = !-[1,]: 这句代码在IE9之前曾被称为世界上最短的IE判定代码.代码虽短但确包含了不少javascript基础知识在里面.在这个例子中代码执行时会先调用数组的toString( ...

  5. onvif规范的实现:使用gSOAP创建SOAP调用实例

    预备知识 ONVIF规范中设备管理和控制部分所定义的接口均以Web Services的形式提供.ONVIF规范涵盖了完全的XML及WSDL的定义.每一个支持ONVIF规范的终端设备均须提供与功能相应的 ...

  6. Letter of application, e-mail version

    Subject line: (logical to recipient!) Application for sales representative for mid-Atlantic area Apr ...

  7. SQL Server Schema

    SQL查询是引用表时,需要为其制定模式名. 在数据库的术语中,模式就是名称空间.这种方式可以把相同特性的对象组合到一个共同的名称空间中.模式也可以保护对象,所以DBA可以给模式授予显示权限. 例如,D ...

  8. 消除多余的row

    tableviewName.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];

  9. java web 之 WebRoot和WebContent目录

    WebRoot和WebContent都是程序的根文件夹,无本质区别,一下是两者的共同点和不同点: 共同点:都有一个WEB-INF文件夹,其下文件不可直接访问: WEB-INF是安全目录,所谓安全,就是 ...

  10. auto 和 decltype (C++11 新增)

    红色字体为个人推断,可信度自辨. 蓝色字体为重点. auto类型说明符:使用auto时,编译器会分析表达式,并自动推算出变量所属类型.*auto变量必须有初值 原理:编译器通过 初值 来判断auto变 ...