codeforces #433C Ryouko's Memory Note

来源:互联网 发布:淘宝特卖9.9 编辑:程序博客网 时间:2024/05/22 06:59

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 page xto 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 61 2 3 4 3 2
output
3
input
10 59 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.

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 110000;vector<int> q[maxn];ll n,m,a[maxn];int i,j;ll ansmin,temp;ll sum;ll maxx=-1;int main(){    cin>>n>>m;sum=0;    for(int i=0;i<m;i++)    {        cin>>a[i];        maxx=max(maxx,a[i]);        if(i!=0&&a[i]!=a[i-1])        {            q[a[i]].push_back(a[i-1]);            q[a[i-1]].push_back(a[i]);            sum+=abs(a[i]-a[i-1]);        }    }    ansmin=sum;    for(int i=1;i<=maxx;i++)    {        temp=sum;        int len=q[i].size();        if(len==0)continue;        sort(q[i].begin(),q[i].end());        ll x=q[i][len/2];        for(j=0;j<len;j++)        {            temp+=abs(x-q[i][j])-abs(i-q[i][j]);        }        ansmin=min(ansmin,temp);    }    cout<<ansmin<<endl;}



题目地址:http://codeforces.com/problemset/problem/433/C


这题的思路是把每一个数的与之相邻的保存下来,为了方便,可以用vector数组。然后为了使得距离之和最短,要取中位数。在一串数字中,距所有数字距离之和最短的就是中位数了,这点应该很好理解。然后把该值修改为该中位数,然后最后找出能使值减少的最大的就是最终要修改的值。

代码如下:



原创粉丝点击