Helping the Nature
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 n trees growing near the path, the current levels of moisture of each tree are denoted by the array a1,a2,…,an1,2,…,. Leon has learned three abilities which will help him to dry and water the soil.
- Choose a position i and decrease the level of moisture of the trees 1,2,…,i1,2,…, by 11.
- Choose a position i and decrease the level of moisture of the trees i,i+1,…,n,+1,…, 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.
The first line contains a single integer t (1≤t≤2⋅1041≤≤2⋅104) — the number of test cases. The description of t test cases follows.
The first line of each test case contains a single integer n (1≤n≤2000001≤≤200000).
The second line of each test case contains n integers a1,a2,…,an1,2,…, (−109≤ai≤109−109≤≤109) — the initial levels of trees moisture.
It is guaranteed that the sum of n over all test cases doesn't exceed 200000200000.
For each test case output a single integer — the minimum number of actions. It can be shown that the answer exists.
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.
用一个变量 a 来记录把所有树统一成同一个高度时所需的操作次数。
当我们遇到一棵树高为 ℎh 的树的时候,有以下两种可能的情况:
-
如果 ℎ≤a 那么把前面的树操作 h−a 次使高度统一。
-
否则把在这棵树上操作 a−h 次把它的高度将为 a。
我们发现在两种情况下我们都要操作 ∣h−a∣ 次,最后不要忘记给答案再加上∣a∣ 次操作使得所有树的高度都变为 0。
__EOF__

本文链接:https://www.cnblogs.com/o-Sakurajimamai-o/p/17480269.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)