First find the next difference between the largest, the second largest, the third largest
Deleting the end point will reduce a value, delete the remaining points will be reduced by two values, a new value, so the new and existing maximum value comparison of the maximum can be
1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 #defineLL Long Long6 Const intN =100005;7 intT, N, p1, p2, p3;8 LL A[n];9 LL S1[n], s2[n];Ten LL sum; One intMain () A { -scanf"%d", &t); - while(t--) the { -scanf"%d", &n); - for(inti =1; I <= N; i++) -scanf"%i64d", &a[i]); +P1 = P2 = P3 =0; -s1[0] = -1; + for(inti =1; I < n; i++) A { atS1[i] = ABS (A[i +1]-a[i]);//adjacent - if(S1[P1] < s1[i]) P1 = i;//Maximum - } - for(inti =1; I < n; i++) - { - if(i = = p1)Continue; in Else if(S1[P2] < s1[i]) P2 = i;//second largest - } to for(inti =1; I < n; i++) + { - if(i = = P1 | | i = p2)Continue;//The third largest the Else if(S1[P3] < s1[i]) P3 =i; * } $ for(inti =2; I < n; i++)Panax NotoginsengS2[i] = ABS (a[i+1]-a[i-1]);//Remove I point new value - thesum =0; + if(P1 = =1) Sum + =S1[P2]; A ElseSum + =S1[P1]; the for(inti =2; I < n; i++) + { - if(P1 = =-1|| P1 = =i) $ { $ if(P2 = =-1|| P2 = = i) sum + =Max (S1[P3], s2[i]); - ElseSum + =Max (S1[P2], s2[i]); - } the ElseSum + =Max (S1[P1], s2[i]); - }Wuyi if(P1 = = N-1) Sum + =S1[P2]; the ElseSum + =S1[P1]; -printf"%i64d\n", sum); Wu } -}
HDU 5805-nanoape Loves Sequence (bestcoder Round #86)