D. Insert a Progression(数学)

You are given a sequence of nn integers a1,a2,…,ana1,a2,…,an. You are also given xx integers 1,2,…,x1,2,…,x.

You are asked to insert each of the extra integers into the sequence aa. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence.

The score of the resulting sequence a′a′ is the sum of absolute differences of adjacent elements in it (∑i=1n+x−1|ai′−ai+1′|).

What is the smallest possible score of the resulting sequence a′a′?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains two integers nn and xx (1≤n,x≤2⋅1051≤n,x≤2⋅105) — the length of the sequence and the number of extra integers.

The second line of each testcase contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105).

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print a single integer — the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it.

Example

input

Copy

4
1 5
10
3 8
7 2 10
10 2
6 1 5 7 3 3 9 10 10 1
4 10
1 3 1 2

output

Copy

9
15
31
13

Note

Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score.

  • 1–,2–,3–,4–,5–,101_,2_,3_,4_,5_,10
  • 7–,7,6–,4–,2,2–,1–,3–,5–,8–,107_,7,6_,4_,2,2_,1_,3_,5_,8_,10
  • 6,1–,1,2–,5,7,3,3,9,10,10,16,1_,1,2_,5,7,3,3,9,10,10,1
  • 1,3,1–,1,2,2–,3–,4–,5–,6–,7–,8–,9–,10–––1,3,1_,1,2,2_,3_,4_,5_,6_,7_,8_,9_,10_

题意:

 给一个长度为n的数组a,再给你一个数字k,你要将1~k个数字插入到a数组中,使得(∑i=1n+x−1|ai′−ai+1′|)值最小。

 思路:

我们先找到数组a中的最大值ma和最小值mi,我们可以证明:不论你在ma和mi之间插入多少个数字,这段区间的答案并没有影响,同理如果k>ma,我们在k~ma之间插入任意多个数字,不影响这段区间的答案,还有就是在1~mi之间插入任意多个数字,也不影响这段区间的答案。

例如|:mi = 1, ma = 10;区间的答案为9; 1,2,3,8,10;答案仍为9,

所以我们最初的答案就是求数组a两两差值的绝对,最后在判断k和ma的大小,以及1~a[1],的区间最小值

 #include <bits/stdc++.h>

 #define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
 #define int long long

 using namespace std;

 const int N = 2e5 + 10;

 int n, k;
 int arr[N];

 void solve()
 {
      cin >> n >> k;
      int mi = 1e9, ma = -1;
      for(int i = 1; i <= n; i ++)
      {
           cin >> arr[i];
           mi = min(arr[i], mi);
           ma = max(arr[i], ma);
      }

      int ans = 0;

      for(int i = 2; i <= n; i ++)
           ans += abs(arr[i]-arr[i-1]);

      ans += min({arr[1]-1, arr[n]-1, 2*(mi-1)});

      if(ma < k)
          ans += min({k-arr[1], k-arr[n], 2*(k-ma)});
      cout << ans << '\n';
 }

 signed main()
 {
      IOS;
      int T;
      cin >> T;
      while(T--)
          solve();
      return 0;
 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值