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

Sample Input

4 6
1 2 3 4 3 2

Sample Output

3

Hint

题意

有一个人,他在读书,然后他会依次翻页,现在他可以使用一次魔法,把可以把第x页合并在第y页上

问你至多使用一次魔法的情况下,他最少翻页多少页?

题解:

对于某一页,他如果变了的话,就只会影响要翻到他的页数,和他要翻到的页数

把这些都存下来,然后显然取个中位数就好了

所以直接这样暴力莽一波就好了

复杂度是nlogn的,均摊了一下

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,m,a[maxn];
vector<int>near[maxn];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)
    {
        if(i!=1&&a[i]!=a[i-1])
            near[a[i]].push_back(a[i-1]);
        if(i!=m&&a[i]!=a[i+1])
            near[a[i]].push_back(a[i+1]);
    }
    long long ans = 0;
    for(int i=1;i<m;i++)
        ans+=abs(a[i]-a[i+1]);
    long long Ans=ans;
    for(int i=1;i<=n;i++)
    {
        if(near[i].size()==0)continue;
        long long tmp=0;
        for(int j=0;j<near[i].size();j++)
            tmp+=abs(near[i][j]-i);
        sort(near[i].begin(),near[i].end());
        long long p = near[i][near[i].size()/2];
        long long tmp2=0;
        for(int j=0;j<near[i].size();j++)
            tmp2+=abs(near[i][j]-p);
        Ans=min(ans-tmp+tmp2,Ans);
    }
    cout<<Ans<<endl;
}

转载于:https://www.cnblogs.com/qscqesze/p/5557465.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值