Handles the maximum value to the left of each location and the maximum value to the right. Then the O (1) can be calculated to remove the maximum value of a position.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-8;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} InlineintRead () {Charc = GetChar (); while(!isdigit (c)) C =GetChar (); intx =0; while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();} returnx;}Const intmaxn=100000+Ten;intT,N,A[MAXN],L[MAXN],R[MAXN]; LL ans;intMain () {scanf ("%d",&T); while(t--) {ans=0; memset (L,0,sizeofL); memset (R,0,sizeofR); scanf ("%d", &n); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); for(intI=2; i<=n;i++) L[i]=max (l[i-1],abs (a[i]-a[i-1])); for(inti=n-1; i>=1; i--) R[i]=max (r[i+1],abs (a[i]-a[i+1])); for(intI=1; i<=n;i++) { if(i==1) ans=ans+r[2]; Else if(i==n) ans=ans+l[n-1]; ElseAns=ans+max (l[i-1],max (r[i+1],abs (a[i-1]-a[i+1]))); } printf ("%lld\n", ans); } return 0;}
HDU 5805 nanoape Loves Sequence