题目链接:

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. Framewrok损坏导致卸载不了的解决办法

    1 使用微软自己提供的卸载软件 msicuu2.exe 卸载所有Framework安装版本和更新信息 2 卸载成功后,最好重启一下电脑,然后,从低版本到高版本,安装framewrok,当然,这个按照个 ...

  2. visual studio 设计器上出现蓝色的点和箭头

    visual studio 设计器上出现蓝色的点和箭头: 突然发现打开visual studio 的时候出现了很多蓝色的点和箭头,解决办法是:按组合快捷键 Ctrl+R,Ctrl+W 或 Ctrl+E ...

  3. mvc web api 保存多个实体类的方法

    先说一下我的业务需求,挺简单的. 就是在设计角色和权限的页面中,需要一个角色对应多条权限记录,那么在保存的时候,我的处理方式是,先删除该角色的所有权限,然后再保存所有的权限. 这样的话问题就来了,用默 ...

  4. HTML第四天学习笔记

    今天老师教了下无序列表和有序列表,虽然并没有,同时做了个随堂练习: <html> <head> <title>随堂练习00</title> <me ...

  5. 系列文章--精通CSS.DIV网页样式与布局学习

    精通CSS.DIV网页样式与布局(八)——滤镜的使用 精通CSS.DIV网页样式与布局(七)——制作实用菜单 精通CSS.DIV网页样式与布局(六)——页面和浏览器元素 精通CSS.DIV网页样式与布 ...

  6. apache2 添加perl支持

    官方文档,很详细了. https://httpd.apache.org/docs/2.0/howto/cgi.html#configuring   我修改了/etc/apache2/sites-ava ...

  7. [Express] Level 4: Body-parser -- Post

    Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...

  8. 关于Android圆形图片的一种优化方案(可以显示网络图片)

    在Android App中,我们经常看到圆形头像图片,然后网上也有很多开源的控件.刚好这个项目用到了,也去找了一些开源的,发现并不完美,所以只好自己优化了,废话不多说,先上效果图: 下面是源码:本人能 ...

  9. C++ Socket超时设置

    用winsocket时,send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,可以设置收发时限:int nNetTimeout = 1000; //1秒//发送时限setsocko ...

  10. CSS 居中大全【转】

    我看最近微博流行 CSS 居中技术,老外码农争相写相关的文章,一篇赛一篇的长啊,我把几篇归纳总结了一下,算是笔记. 孔乙己曾说:“茴香豆的回字有四种写法”,万一哪天有个面试官问你:“居中一共有几种写法 ...