A. Ryouko's Memory Note

题目连接:

http://www.codeforces.com/contest/434/problem/A

Description

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 Input

4 6

1 2 3 4 3 2

Sample Output

3

Hint

题意

有一个人,他在读书,然后他会依次翻页,现在他可以使用一次魔法,把可以把第x页合并在第y页上

问你至多使用一次魔法的情况下,他最少翻页多少页?

题解:

对于某一页,他如果变了的话,就只会影响要翻到他的页数,和他要翻到的页数

把这些都存下来,然后显然取个中位数就好了

所以直接这样暴力莽一波就好了

复杂度是nlogn的,均摊了一下

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,m,a[maxn];
vector<int>near[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
if(i!=1&&a[i]!=a[i-1])
near[a[i]].push_back(a[i-1]);
if(i!=m&&a[i]!=a[i+1])
near[a[i]].push_back(a[i+1]);
}
long long ans = 0;
for(int i=1;i<m;i++)
ans+=abs(a[i]-a[i+1]);
long long Ans=ans;
for(int i=1;i<=n;i++)
{
if(near[i].size()==0)continue;
long long tmp=0;
for(int j=0;j<near[i].size();j++)
tmp+=abs(near[i][j]-i);
sort(near[i].begin(),near[i].end());
long long p = near[i][near[i].size()/2];
long long tmp2=0;
for(int j=0;j<near[i].size();j++)
tmp2+=abs(near[i][j]-p);
Ans=min(ans-tmp+tmp2,Ans);
}
cout<<Ans<<endl;
}

Codeforces Round #248 (Div. 1) A. Ryouko's Memory Note 水题的更多相关文章

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

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

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

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

  3. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  4. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  5. Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题

    A. Whose sentence is it? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  6. Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题

    A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...

  7. Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题

    A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...

  8. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  9. Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题

    B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

随机推荐

  1. 揭秘Patchwork APT攻击-恶意软件样本BADNEWS

    1.前言 在2016年左右研究人员发现一个与东南亚和中国南海问题的APT攻击,该APT攻击利用MS Offcie系列漏洞通过钓鱼邮件的形式欺骗受害者点击木马.以美国在内的各国政府和公司为目标发送了大量 ...

  2. FPGA设计方法检查表

    -----------------------摘自<FPGA软件测试与评价技术> 中国电子信息产业发展研究院 | 编著------------------------------- 文本格 ...

  3. MySQL5.7之多源复制&Nginx中间件(下)【转】

    有生之年系列----MySQL5.7之多源复制&Nginx中间件(下)-wangwenan6-ITPUB博客http://blog.itpub.net/29510932/viewspace-1 ...

  4. TCP/IP详解(整理)

    1.概述 路由器是在网络层进行联通,而网桥是在链路层联通不同的网络. IP层用ICMP来与其他主机或路由器交换错误报文和其他的重要信息.应用程序也可以访问ICMP,两个诊断工具:Ping和Tracer ...

  5. scala可变长度参数(转)

    可变长度参数 Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表.想要标注一个重复参数,在参数的类型之后放一个星号.例如: scala> def ec ...

  6. robotframework-ride多次运行,有时候不显示日志信息

    解决方法: 修改"C:\Python27\lib\site-packages\robotide\contrib\testrunner\testrunner.py"文件pop方法中  ...

  7. Git push将本地版本库的分支推送到远程服务器上对应的分支

    在使用git commit命令将修改从暂存区提交到本地版本库后,只剩下最后一步将本地版本库的分支推送到远程服务器上对应的分支了,如果不清楚版本库的构成,可以查看我的另一篇,git 仓库的基本结构. g ...

  8. python_接口自动化测试框架

    本文总结分享介绍接口测试框架开发,环境使用python3+selenium3+unittest+ddt+requests测试框架及ddt数据驱动,采用Excel管理测试用例等集成测试数据功能,以及使用 ...

  9. GitHub正式启用声明

    0x00 说明 GitHub账号很早之前就已经注册,几经常识都没有能够好好的将这座宝库应用好,实在是汗颜.今天特地将GitHub重新拾起,以后一定要好好使用这个利器,不要荒废了. 0x01 内容 Gi ...

  10. 【51nod】1564 区间的价值

    题解 这个要注意到一个长度大的区间的最大价值一定比长度小的区间的价值要大 然后我们以每个点为最小值,显然区间越长最大值越大,然后我们更新最大区间长度的取值,这个可以用单调栈求这个最小值能更新到的左右端 ...