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

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



解题思路:

有点类似暴力又有点类似贪心。比较简单,认真读题。

先将merge前的翻页数算出来,再将每一个元素的前后分别插入到以该元素为脚标的动态数组(最好别用二维数组,后面的一些写法貌似不太适合用二维的)里面。将此动态数组排序,并取中位数(贪心:求所有元素相对某一元素距离差的绝对值的和,最小点是中位数,证明见刘汝佳白书),然后再计算改变量,取最优解。以上步奏重复1~n的每一个元素,得到的就是答案。


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int MAXN=100100;  
typedef long long int ll;  
  
int n,m;  
ll a[MAXN];  
vector<int> near[MAXN];  
  
int main()  
{  
    scanf("%d %d",&n,&m); 
    for(int i=0;i<m;i++)  
        scanf("%d",&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 s=near[i].size();  
        if(s)  
        {  
            sort(near[i].begin(),near[i].end());  
            ll change=0;int mid=near[i][s/2];  
            for(int j=0;j<s;j++)  
                change+=abs(near[i][j]-mid)-abs(near[i][j]-i);   
            cur=min(cur,orz+change);  
        }  
    }  
    printf("%I64d\n",cur); 
    return 0;  
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值