题目链接:

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. Android与.Net交互模拟用户屏幕操作添加APN和网络4G/3G切换

    前几天接到一个需求,我们的客户需要对手机网络接入点进行可用性测试,简单点说就是需要实现Android上的APN配置的添加,APN切换网络模式4G/3G/2G切换,我要调研下写个demo. 因为是要实现 ...

  2. Java工具类 Apache Commons:commons-lang

    Commons Lang The standard Java libraries fail to provide enough methods for manipulation of its core ...

  3. DLL文件无法删除怎么解决

    dll文件你听说过吗?那怎样把那些删不掉的东西删掉呢?请看.... 老听网友说某某文件删不掉啊.之类的.而且有很多都是dll文件.删除的时候总是提示,"正在使用"或者是" ...

  4. wpa_supplicant 连接成功后,如何配置wlan0与br0 协调上网

    wlan0 地址,路由配置完成后,加入两条iptables 规则. #iptables -A FORWARD -i wlan0 -o br0 -s -m state --state NEW -j AC ...

  5. 从零开始学android开发-项目打包发布

    右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...

  6. Android中Webview使用javascript调用事先定义好的Java函数

    1. 首先定义好一个类,专们用于给javascript调用 public class JavaScriptInterface { // share your news public void shar ...

  7. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  8. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  9. 用JSP实现的商城购物车模块

    这两天,在学习JSP,正好找个小模块来练练手: 下面就是实现购物车模块的页面效果截图: 图1. 产品显示页面 通过此页面进行产品选择,增加到购物车 图2 .购物车页面 图3 . 商品数量设置 好了,先 ...

  10. 深入理解JavaScript 模块模式

    http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html 模块模式是JavaScript一种常用的编码模式.这是一般的 ...