Topic Links:

Nanoape Loves Sequence

**Time limit:2000/1000 MS (java/others)**

**Memory limit:262144/131072 K (java/others)**

Problem Descriptionnanoape, the retired Dog, had returned back to prepare for the national higher Education Entrance Exami nation!

In the math class, Nanoape picked up sequences once again. He wrote down a sequence withn numbers on the paper and then randomly deleted a number in the sequence. After that, he calculated the maximum absolute value of the difference of each of the adjacent remained numbers, denoted as c4>F.

Now he wants to know the expected value of F, if he deleted each number with equal probability.

Inputthe first line of the input contains an integerT, denoting the number of test cases.

In each test case, the first line of the input contains an integerN, denoting the length of the original sequence.

The second line of the input containsNIntegersa1,a2,.. . ,An , denoting the elements of the sequence.

1≤t≤ 10, 3≤n≤100000, 1≤ a I≤ 109

Outputfor each test case, print a line with one integer, denoting the answer.

In order to prevent using float number, you should print the answer multiplied by n.

Sample Input141 2 3 4

Sample Output6 Test Instructions: Ask what is the expectation of getting rid of one number and the difference of the number of neighbors? Thought: Originally very simple, I foolishly wrote the line segment tree; AC Code:

/************************************************┆┏┓┏┓┆┆┏┛┻━━━┛┻┓┆┆┃┃┆┆┃━┃┆┆┃┳┛┗┳┃┆┆┃┃ ┆┆┃┻┃┆┆┗━┓┏━┛┆┆┃┃┆┆┃┗━━━┓┆┆┃ac Horse ┣┓┆┆┃┏┛┆┆┗┓┓┏━┳┓┏┛┆┆┃┫┫┃┫┫┆ ┆┗┻┛┗┻┛┆************************************************ * * #include <iostream> #include <cstdio># Include <cstring> #include <algorithm> #include <cmath>//#include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL; Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) Stk[+ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E9;CONST int N=1e5+10;const int Maxn=2e3+14;const double eps= 1e-12;int a[n],b[n];struct node{int l,r,s;} tr[4*n];void Build (int o,int l,int R) {tr[o].l=l;tr[o].r=r; if (l>=r) {tr[o].s=b[l]; return; } int mid= (L+R) >>1; Build (2*o,l,mid); Build (2*o+1,mid+1,r); Tr[o].s=max (TR[2*O].S,TR[2*O+1].S);} void update (int o,int x,int num) {if (tr[o].l==tr[o].r&&tr[o].l==x) {tr[o].s=num; return; } int mid= (TR[O].L+TR[O].R) >>1; if (x<=mid) update (2*o,x,num); else update (2*o+1,x,num); Tr[o].s=max (TR[2*O+1].S,TR[2*O].S);} int query (int o,int l,int R) {if (tr[o].l>=l&&tr[o].r<=r) return tr[o].s; int mid= (TR[O].R+TR[O].L) >>1; if (r<=mid) return query (2*O,L,R); else if (l>mid) return query (2*O+1,L,R); else return max (query (2*O,L,MID), query (2*o+1,mid+1,r));} int main () {int t; Read (t); while (t--) {int n; Read (n); for (i,1,n) read (A[i]); for (i,1,n-1) B[i]=abs (A[i+1]-a[i]); Build (1,1,n-1); LL ans=0; for (I,1,n) {if (i==1) {update (1,1,0); Ans=ans+query (1,1,n-1); Update (1,1,b[1]); } else if (i==n) {update (1,n-1,0); Ans=ans+query (1,1,n-1); Update (1,n-1,b[n-1]); } else {update (1,i-1,0); Update (1,i,abs (a[i+1]-a[i-1)); Ans=ans+query (1,1,n-1); Update (1,i,b[i]); Update (1,i-1,b[i-1]); }} cout<<ans<< "\ n"; } return 0;}

hdu-5805 nanoape Loves Sequence (segment tree + probabilistic expectation)