Educational Codeforces Round 127 D. Insert a Progression

Insert a Progression

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given a sequence of n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an. You are also given x x x integers 1 , 2 , … , x 1, 2, \dots, x 1,2,,x.

You are asked to insert each of the extra integers into the sequence a a a. 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' a is the sum of absolute differences of adjacent elements in it ( ∑ i = 1 n + x − 1 ∣ a i ′ − a i + 1 ′ ∣ ) \left(\sum \limits_{i=1}^{n+x-1} |a'_i - a'_{i+1}|\right) (i=1n+x1aiai+1).

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

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of testcases.

The first line of each testcase contains two integers n n n and x x x ( 1 ≤ n , x ≤ 2 ⋅ 1 0 5 1 \le n, x \le 2 \cdot 10^5 1n,x2105) — the length of the sequence and the number of extra integers.

The second line of each testcase contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 2 ⋅ 1 0 5 1 \le a_i \le 2 \cdot 10^5 1ai2105).

The sum of n n n over all testcases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

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

i n p u t \tt input input
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
o u t p u t \tt output output
9
15
31
13

Tutorial

由题意易得,对于数组 a a a 中已有的数不能改变,此时差值为 ∑ i = 1 ∣ a ∣ a b s ( a i − a i − 1 ) \sum_{i = 1}^{|a|}abs(a_i - a_{i - 1}) i=1aabs(aiai1),其中 ∣ a ∣ |a| a 表示数组 a a a 的长度

在任意两个数 a i a_i ai a i + 1 a_{i + 1} ai+1 之间,满足 a i ≤ x ≤ a i + 1 a_i \le x \le a_{{i + 1}} aixai+1 条件的 x x x 对答案都不会产生影响,因此插入满足 min ⁡ ( a ) ≤ x ≤ max ⁡ ( a ) \min(a) \le x \le \max(a) min(a)xmax(a) 条件的 x x x 均不会对答案产生影响,我们只需要考虑小于 min ⁡ ( a ) \min(a) min(a) 的数和大于 max ⁡ ( a ) \max(a) max(a) 的数对答案会产生什么影响即可

对于小于 min ⁡ ( a ) \min(a) min(a) 的数:

  • 如果将 1 , 2 , … , min ⁡ ( a ) − 1 1,2,\dots,\min(a) - 1 1,2,,min(a)1 正序插入到整个数列 a a a 的最前方,产生的影响为 a b s ( a 0 − 1 ) abs(a_0 - 1) abs(a01)
  • 如果将 min ⁡ ( a ) − 1 , min ⁡ ( a ) − 2 , … , 2 , 1 \min(a) - 1, \min(a) - 2, \dots, 2, 1 min(a)1,min(a)2,,2,1 倒序插入到整个数列 a a a 的最后方,产生的影响为 a b s ( a ∣ a ∣ − 1 ) abs(a_{|a|} - 1) abs(aa1),其中 ∣ a ∣ |a| a 表示数组 a a a 的长度
  • 如果将 1 , 2 , … , min ⁡ ( a ) − 1 1,2,\dots,\min(a) - 1 1,2,,min(a)1 按序插入到数组 a a a 中间,产生的影响为 ( min ⁡ ( a ) − 1 ) × 2 (\min(a) - 1) \times 2 (min(a)1)×2

对于大于 max ⁡ ( a ) \max(a) max(a)​ 的数:

  • 如果将 x − 1 , x − 2 , … , max ⁡ ( a ) + 1 x - 1, x - 2, \dots, \max(a) + 1 x1,x2,,max(a)+1 倒序插入到整个数列 a a a 的最前方,产生的影响为 a b s ( x − a 0 ) abs(x - a_0) abs(xa0)
  • 如果将 max ⁡ ( a ) + 1 , max ⁡ ( a ) + 2 , … , x \max(a) + 1,\max(a) + 2,\dots,x max(a)+1,max(a)+2,,x 正序插入到整个数列 a a a 的最后方,产生的影响为 a b s ( x − a ∣ a ∣ ) abs(x - a_{|a|}) abs(xaa),其中 ∣ a ∣ |a| a 表示数组 a a a 的长度
  • 如果将 1 , 2 , … , min ⁡ ( a ) − 1 1,2,\dots,\min(a) - 1 1,2,,min(a)1 按序插入到数组 a a a 中间,产生的影响为 ( x − max ⁡ ( a ) ) × 2 (x - \max(a)) \times 2 (xmax(a))×2,当然,如果 max ⁡ ( a ) > x \max(a) > x max(a)>x,此时影响为 0

综上所述,对于每一个数组 a a a x x x,答案为

{ ∑ i = 1 ∣ a ∣ a b s ( a i − a i − 1 ) min ⁡ ( a b s ( x − a 0 ) , a b s ( x − a ∣ a ∣ ) , max ⁡ ( 0 , ( x − max ⁡ ( a ) ) × 2 ) ) min ⁡ ( a b s ( a 0 − 1 ) , a b s ( a ∣ a ∣ − 1 ) , ( min ⁡ ( a ) − 1 ) × 2 ) \left\{\begin{matrix} \sum_{i = 1}^{|a|}abs(a_i - a_{i - 1}) \\ \min(abs(x - a_0), abs(x - a_{|a|}), \max(0, (x - \max(a)) \times 2)) \\ \min(abs(a_0 - 1), abs(a_{|a|} - 1), (\min(a) - 1) \times 2) \end{matrix}\right. i=1aabs(aiai1)min(abs(xa0),abs(xaa),max(0,(xmax(a))×2))min(abs(a01),abs(aa1),(min(a)1)×2)

的和

此解法时间复杂度为 O ( n ) \mathcal O(n) O(n),即求 ∑ i = 1 ∣ a ∣ a b s ( a i − a i − 1 ) \sum_{i = 1}^{|a|}abs(a_i - a_{i - 1}) i=1aabs(aiai1) 时的时间复杂度

Solution

import sys
input = lambda: sys.stdin.readline().strip()

output = []

for _ in range(int(input())):
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = sum(abs(a[i] - a[i + 1]) for i in range(n - 1))
    mx, mn = max(a), min(a)
    up = min(abs(x - a[0]), abs(x - a[-1]), (0 if mx > x else (x - mx) * 2))
    down = min(abs(a[0] - 1), abs(a[-1] - 1), (mn - 1) * 2)
    output.append(ans + up + down)

print('\n'.join(map(str, output)))
  • 28
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值