CF2179B.Blackslex and Showering

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

After his IMO medalist friend showered for two hours so that "he doesn't have to shower this again this week," Blackslex will be late for class!

In order to get to class, Blackslex must take the crowded elevator to many floors in a certain order. Because he is a hacker, he can skip visiting up to one floor without the other people knowing. His time taken is the sum of the absolute differences between consecutive floor numbers. Find the minimum time taken, given that he can skip up to one floor.

More formally, given an array a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n] of nn integers, you can choose up to one index k∈1,2,…,nk \in {1, 2, \ldots, n} to erase such that the sum $$\sum_{i=1}^{n-2} |b_i - b_{i+1}|$$ is minimized, where b=[a1,…,ak−1,ak+1,…,an]b = [a_1, \ldots, a_{k-1}, a_{k+1}, \ldots, a_n] is the array after erasing element aka_k. Report the minimum sum.

在他那位国际数学奥林匹克竞赛(IMO)金牌得主朋友“为了本周不用再洗澡”而洗了两个小时的澡之后,Blackslex 上课要迟到了!

为了赶去上课,Blackslex 必须乘坐拥挤的电梯,按特定顺序经过多个楼层。由于他是一名黑客,他最多可以偷偷跳过一个楼层而不被其他人察觉。他所花费的时间等于相邻楼层号之间绝对差值的总和。在最多跳过一个楼层的前提下,求他所需的最短时间。

更形式化地,给定一个包含 nn 个整数的数组 a=[a1,a2,…,an]a = [a_1, a_2, \ldots, a_n],你可以至多选择一个下标 k∈{1,2,…,n}k \in \{1, 2, \ldots, n\} 将元素 aka_k 删除,使得删除后得到的数组 b=[a1,…,ak−1,ak+1,…,an]b = [a_1, \ldots, a_{k-1}, a_{k+1}, \ldots, a_n] 满足和式

∑i=1n−2∣bi−bi+1∣\sum_{i=1}^{n-2} |b_i - b_{i+1}|

最小。请输出该最小和。

输入格式

The first line contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains one integer nn (3≤n≤2⋅1053 \le n \le 2 \cdot 10^5) — the size of the array.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1001 \le a_i \le 100).

It is guaranteed that the sum of nn does not exceed 2⋅1052 \cdot 10^5 over all test cases.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 数组的大小。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1001 \le a_i \le 100)。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single real number — the minimum time taken.

对于每个测试用例,输出一个实数——所需的最短时间。

输入输出样例

  • 输入#1

    3
    5
    4 15 1 7 9
    3
    2 4 8
    6
    11 13 17 19 23 29

    输出#1

    11
    2
    12

说明/提示

For the first test case, one optimal index to remove from [4,15,1,7,9][4, 15, 1, 7, 9] is k=2k = 2. The array becomes [4,1,7,9][4, 1, 7, 9] and the time taken is 1111. For the second test case, the optimal index to remove is k=3k = 3.

对于第一个测试用例,从数组 [4,15,1,7,9][4, 15, 1, 7, 9] 中移除的最优下标为 k=2k = 2。数组变为 [4,1,7,9][4, 1, 7, 9],所用时间为 1111。对于第二个测试用例,最优移除下标为 k=3k = 3。

输入解题思路,AI测评打分。不知道怎么写?

首页