题目链接:

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

 
Examples
 
input
4 6
1 2 3 4 3 2
output
3
input
10 5
9 4 3 8 8
output
6

题意:

a[i]表示a[i]页,按顺序翻页,现在可以把其中一页的内容全部复制到另一页,问最少翻的页数;

思路:

假设把第x页的内容复制走,|a[i]-x|+|a[j]-x|+...+|a[k]-x|,排序后去掉绝对值得x-a[i]+x-a[j]+...+a[k]-x;把x变成a[i],a[j],...a[k]的中位数就可以得到最小值;
其中a[i],a[j]...a[k]这些数是与x相邻的的页数,有个wa点就是要把一开始相邻的相同的页数合并,因为这个wa了好多发; AC代码:
#include <bits/stdc++.h>
/*#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=1e5+; int n,m;
int a[N],vis[N],c[N];
LL b[N],sum=;
vector<int>ve[N]; int main()
{
read(n);read(m);
int cnt=;
Riep(m)
{
read(c[i]);
if(c[i]!=c[i-])a[++cnt]=c[i],vis[c[i]]++;
}
m=cnt;
for(int i=;i<=m;i++)sum=sum+abs(a[i]-a[i-]); Riep(m)
{
if(i==)ve[a[i]].push_back(a[]),b[a[i]]=b[a[i]]+abs(a[]-a[]);
else if(i==m)ve[a[i]].push_back(a[m-]),b[a[i]]=b[a[i]]+abs(a[m]-a[m-]);
else ve[a[i]].push_back(a[i-]),ve[a[i]].push_back(a[i+]),b[a[i]]=b[a[i]]+abs(a[i]-a[i-])+abs(a[i]-a[i+]);
}
LL ans=sum;
if(m>)
for(int i=;i<=n;i++)
{
if(vis[i])
{
sort(ve[i].begin(),ve[i].end());
int len=ve[i].size();
LL s=;
for(int j=;j<len;j++)
{
s=s+abs(ve[i][j]-ve[i][len/]);
}
ans=min(ans,sum-b[i]+s);
}
}
cout<<ans<<"\n";
return ;
}

codeforces 434A A. Ryouko's Memory Note(数学)的更多相关文章

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

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

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

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

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

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

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

  5. codeforces C. Ryouko's Memory Note

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

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

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

  7. Ryouko's Memory Note

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

  8. Codeforces 433 C. Ryouko&#39;s Memory Note

    C. Ryouko's Memory Note time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. CodeForces 433C Ryouko's Memory Note-暴力

                                             Ryouko's Memory Note Time Limit:1000MS     Memory Limit:262 ...

随机推荐

  1. 一个可创建读取日志的管理类(可固定创建2M大小的日志文件)

    这里,将日志管理基类命名为LogManagerBase(抽象类),具体的不同类型的日志可以通过继承完成.该基类可将日志以每个2M的方式存储起来,并可以读取当前正在使用的日志的所有内容. 要实现该基类, ...

  2. 国内外从事CV相关的企业[转]

    提示:本文为笔者原创,转载请注明出处:blog.csdn.net/carson2005 经常碰到朋友问我国内从事计算机视觉(CV)领域的公司的发展情况,产品情况,甚至找工作等问题,这里,我给出自己收集 ...

  3. Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE]

    在Android studio中想要运行程序,点击运行后程序安装失败,报出如下异常: 原因:在manifest中设置了 .android:sharedUserId="android.uid. ...

  4. PHP 获取js中变量的方法

    留作笔记 <php? $aaa="<script>document.write(1)</script>"; echo $aaa; ?> 其中数字 ...

  5. 为DELL inspiron 14R安装CentOS X64 6.4

    今天接到一个任务,为DELL inspiron 14R安装CentOS X64 6.4,本以为这是小菜一碟,不算个啥,大大小小安装LINUX CENTOS 不下上百次了,还怕这个?结果,各种碰壁... ...

  6. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...

  7. .cmd文件不小心管理记事本打开的恢复

    比如不小心将.cmd文件关联成用记事本打开了,此时须要删除注冊表: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explor ...

  8. Maven实战之Quick Start

    Introduction Maven是一个异常强大的构建工具,能够帮我们自动化构建过程,从清理.编译.测试到生成报告,再到打包和部署.通过Maven,我们只需要输入简单的命令(如mvn clean i ...

  9. Android Client and Jsp Server

    1. Interestfriend Server https://github.com/eltld/Interestfriend_server https://github.com/774663576 ...

  10. [Ember] Ember.js Templates

    In this lesson, we'll go over some of the basics of Ember.js templates and how they work with contro ...