Helping the Nature

C. Helping the Nature
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains a single integer t (1t21041≤≤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 (1n2000001≤≤200000).

The second line of each test case contains n integers a1,a2,,an1,2,…, (109ai109−109≤≤109) — the initial levels of trees moisture.

It is guaranteed that the sum of n over all test cases doesn't exceed 200000200000.

Output

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

Example
input
Copy
4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5
output
Copy
2 13 36 33
Note

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 那么把前面的树操作 ha 次使高度统一。

  • 否则把在这棵树上操作 ah 次把它的高度将为 a。

我们发现在两种情况下我们都要操作 ha∣ 次,最后不要忘记给答案再加上a∣ 次操作使得所有树的高度都变为 0。

复制代码
//https://www.luogu.com.cn/problem/CF1700C #include<bits/stdc++.h> using namespace std; const int N=2e5+10; long long res,n,h,last_tree,now_tree,t; int main() { cin>>t; while(t--) { cin>>n>>last_tree; h=last_tree,res=0; for(int i=1;i<n;i++) { cin>>now_tree;//读入现在树的高度; res+=abs(last_tree-now_tree);//无论前一棵树比它高还是低,要进行的步骤始终是两者之差的绝对值 if(last_tree>=now_tree) h+=(now_tree-last_tree); //如果这棵树比上颗矮,那么由于上棵树是已经更新之后,也就是减去了一定的高度 //这棵树那必定也会减去相同的高度,那么我们需要维护的高度就变成了 上一颗树变成这棵树所需要的次数加上现在的高度 last_tree=now_tree;//更新现在树的高度 } res+=abs(h); cout<<res<<endl; } return 0; }
复制代码

 

差分:
复制代码
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; long long c[N],res,n,t,p;//long long int main() { cin>>t; while(t--) { cin>>n; res=0; for(int i=1;i<=n;i++) cin>>c[i];//读入 for(int i=2;i<=n;i++) { res+=abs(c[i-1]-c[i]); if(c[i-1]>c[i])c[1]+=(c[i]-c[i-1]);//将i前面的数字全部变成c[i] } cout<<res+abs(c[1])<<endl; } return 0; }
复制代码

 




__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17480269.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
-- --
点击右上角即可分享
微信分享提示