CodeForces - 433C (思维),中位数性质

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

题目给你一个数组,可以改变一个数字(把数组中所有这个数都改成一个数字)。

求操作后,相邻数之间差的绝对值之和最小值。

 

思路,首先要知道有序数组中,中位数到各数的距离之和最小。

数字是1-n,暴力扫一遍要改变的数,然后把这个数组中所有与这个数相邻的数存到另一个数组中(因为不定长,所以这里用vector来储存)。排个序后,这个数与相邻数之差的绝对值和就相当于:求这个数到vector中存的数距离和最小,当然是把它变成中位数后值最小。。。。

 

代码解析:先开二维vector,存i数相邻数的集合。先求不改变时的和。然后暴力扫一遍要改变的数,1-n。把他改成存的相邻数集合的中位数(先进行排序),求改变后的值,求出差值后加上之前的和。  每次求的结果存最小值即可。。

 

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#define maxn 100000+100
using namespace std;
vector<int>ber[maxn];
typedef long long ll;
ll a[maxn];
int main()
{
    ll n,m,sum=0;
    cin>>n>>m;
    for(int i=1;i<=m;i++)
        cin>>a[i];
    for(int i=2;i<=m;i++)
    {
        sum+=abs(a[i]-a[i-1]);
        if(a[i]!=a[i-1])
        {
            ber[a[i]].push_back(a[i-1]);
            ber[a[i-1]].push_back(a[i]);
        }
    }
    ll mi=sum;
    for(int i=1;i<=n;i++)
    {
        if(ber[i].size()==0)
            continue;
        sort(ber[i].begin(),ber[i].end());
        int mid=ber[i][ber[i].size()/2];
        ll ans=sum;
        for(int j=0;j<ber[i].size();j++)
        {
            ans+=abs(ber[i][j]-mid)-abs(ber[i][j]-i);
        }
        mi=min(mi,ans);
    }
    cout<<mi<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树的最大连通块。第二次是自顶向下的过程,处理自底向上过程无法包含的树链所代表的子树。在第二次遍历,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值