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. 网易云课堂_C++开发入门到精通_章节7:模板

    课时35类模板 类模板 创建类模板的实例 class Name<类型参数表>object; 类模板与模板类的区别 类模板是模板的定义,不是一个实实在在的类,定义中用到通用类型参数 模板类是 ...

  2. Train Problem I(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. DICOM:C-GET与C-MOVE对照剖析

    背景: 之前专栏中介绍最多的两款PACS各自是基于dcmtk的dcmqrscp以及Orthanc.和基于fo-dicom的DicomService(自己开发的).该类应用场景都是针对于局域网,因此在使 ...

  4. java的System.getProperty()方法能够获取的值

    java.version Java 执行时环境版本号 java.vendor Java 执行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装 ...

  5. <ListView>分列显示

    <ListView> <ListView.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Width="{B ...

  6. [Python] 发送email的几种方式

    python发送email还是比較简单的,能够通过登录邮件服务来发送,linux下也能够使用调用sendmail命令来发送,还能够使用本地或者是远程的smtp服务来发送邮件,无论是单个,群发,还是抄送 ...

  7. Linux shell中的一个问题 ${}带正则匹配的表达式

    目前在准备龙芯项目的PMON,在研究其编译过程的时候,看到一些make 语句,百思不得其解.后来在shell编程中看到一点资料,牵扯到Shell中的正则表达式.故记录下来,以备后来查阅. 问题: 在某 ...

  8. font-face 使用

    <style type="text/css"> @font-face{ font-family:'Aaargh'; src:url(fonts/Aaargh/Aaarg ...

  9. SFTP CONFIGURATION IN FLASHFXP PROGRAM

    This is a brief guide on how to configure the FlashFXP FTP client to log on to the domain web space ...

  10. tokyocabinet安装日志(持续更新)

    http://sourceforge.jp/projects/sfnet_tokyocabinet/releases/这个网站的最新tt和tc都在此1.下载tokyocabinethttp://sou ...