HDU5805 NanoApe Loves Sequence (BestCoder Round #86 B)前后缀预处理
分析:维护空隙的差,然后预处理前缀最大,后缀最大,扫一遍
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long LL; const int N = 1e5+5; int a[N],T,n,b[N],l[N],r[N]; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%d",&a[i]); LL ret=0; for(int i=1;i<n;++i){ b[i]=abs(a[i]-a[i+1]); l[i]=max(l[i-1],b[i]); } r[n]=0; for(int i=n-1;i>0;--i)r[i]=max(r[i+1],b[i]); for(int i=1;i<=n;++i){ if(i==1)ret+=r[2]; else if(i==n)ret+=l[n-2]; else{ int tmp=max(l[i-2],r[i+1]); tmp=max(tmp,abs(a[i+1]-a[i-1])); ret+=tmp; } } printf("%I64d\n",ret); } return 0; }