CF1700C.Helping the Nature

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees.

There are nn trees growing near the path, the current levels of moisture of each tree are denoted by the array a1,a2,…,ana_1, a_2, \dots, a_n. Leon has learned three abilities which will help him to dry and water the soil.

  • Choose a position ii and decrease the level of moisture of the trees 1,2,…,i1, 2, \dots, i by 11.
  • Choose a position ii and decrease the level of moisture of the trees i,i+1,…,ni, i + 1, \dots, n by 11.
  • Increase the level of moisture of all trees by 11.

Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to 00.

小狮子莱昂住在森林里。他最近注意到,他最喜欢的小径旁的一些树正在枯萎,而另一些树则水分过剩,因此他决定学习如何调控土壤湿度来拯救这些树木。

小径旁共有 nn 棵树,每棵树当前的湿度水平由数组 a1,a2,…,ana_1, a_2, \dots, a_n 表示。莱昂掌握了三种能力,可用于干燥或灌溉土壤:

  • 选择一个位置 ii,将第 1,2,…,i1, 2, \dots, i 棵树的湿度水平各减少 11;
  • 选择一个位置 ii,将第 i,i+1,…,ni, i + 1, \dots, n 棵树的湿度水平各减少 11;
  • 将所有树的湿度水平各增加 11。

莱昂想知道:使每棵树的湿度恰好变为 00 所需的最少操作次数是多少?

输入格式

The first line contains a single integer tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4) — the number of test cases. The description of tt test cases follows.

The first line of each test case contains a single integer nn (1≤n≤200 0001 \leq n \leq 200\,000).

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−109≤ai≤109-10^9 \leq a_i \leq 10^9) — the initial levels of trees moisture.

It is guaranteed that the sum of nn over all test cases doesn't exceed 200 000200\,000.

第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4),表示测试用例的数量。接下来是 tt 个测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤200 0001 \leq n \leq 200\,000)。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109-10^9 \leq a_i \leq 10^9),表示树木初始的湿度水平。

保证所有测试用例中 nn 的总和不超过 200 000200\,000。

输出格式

For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.

对于每个测试用例,输出一个整数——最少操作次数。可以证明答案一定存在。

输入输出样例

  • 输入#1

    4
    3
    -2 -2 -2
    3
    10 4 7
    4
    4 -4 4 -4
    5
    1 -2 3 -4 5

    输出#1

    2
    13
    36
    33

说明/提示

In the first test case it's enough to apply the operation of adding 11 to the whole array 22 times.

In the second test case you can apply the operation of decreasing 44 times on the prefix of length 33 and get an array 6,0,36, 0, 3.

After that apply the operation of decreasing 66 times on the prefix of length 11 and 33 times on the suffix of length 11. In total, the number of actions will be 4+6+3=134 + 6 + 3 = 13. It can be shown that it's impossible to perform less actions to get the required array, so the answer is 1313.

在第一个测试用例中,只需对整个数组执行 22 次加 11 操作即可。

在第二个测试用例中,你可以对长度为 33 的前缀执行 44 次减操作,得到数组 6,0,36, 0, 3。

之后,对长度为 11 的前缀执行 66 次减操作,并对长度为 11 的后缀执行 33 次减操作。总共的操作次数为 4+6+3=134 + 6 + 3 = 13。可以证明,无法通过少于 1313 次操作得到目标数组,因此答案为 1313。

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

首页