HDU5805 NanoApe Loves Sequence (BestCoder Round #86 B)前后缀预处理

时间:2021-06-28 15:00:09

分析:维护空隙的差,然后预处理前缀最大,后缀最大,扫一遍

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+;
int a[N],T,n,b[N],l[N],r[N];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<=n;++i)scanf("%d",&a[i]);
LL ret=;
for(int i=;i<n;++i){
b[i]=abs(a[i]-a[i+]);
l[i]=max(l[i-],b[i]);
}
r[n]=;
for(int i=n-;i>;--i)r[i]=max(r[i+],b[i]);
for(int i=;i<=n;++i){
if(i==)ret+=r[];
else if(i==n)ret+=l[n-];
else{
int tmp=max(l[i-],r[i+]);
tmp=max(tmp,abs(a[i+]-a[i-]));
ret+=tmp;
}
}
printf("%I64d\n",ret);
}
return ;
}