NanoApe Loves Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 440 Accepted Submission(s): 205
In math class, NanoApe picked up sequences once again. He wrote down a sequence with n
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 two adjacent remained numbers, denoted as F
.
Now he wants to know the expected value of F
, if he deleted each number with equal probability.

, denoting the number of test cases.
In each test case, the first line of the input contains an integer n
, denoting the length of the original sequence.
The second line of the input contains n
integers A1
,A
2
,...,A
n
, denoting the elements of the sequence.
1≤T≤10, 3≤n≤100000, 1≤Ai
≤10
9
In order to prevent using float number, you should print the answer multiplied by n
.
4
1 2 3 4
#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; int main()
{
int t;
int n;
int cha=;
int cha2=;
int a[]={};
int maxx1;
int first;
int maxx2;
int second;
int maxx3;
int third;
int sum=;
scanf("%d",&t);
for(int z=;z<t;z++){
sum=;
maxx1=;
maxx2=;
maxx3=;
scanf("%d",&n); for(int i=;i<n;i++){
scanf("%d",&a[i]);
if(i!=){
cha=abs(a[i]-a[i-]);
if(maxx1<cha){
maxx1=cha;
first=i;
}
}
} for(int i=;i<n;i++){
cha=abs(a[i]-a[i-]);
if(maxx2<cha){
if(maxx2<=maxx1){
if(i==first){
continue;
}else{
maxx2=cha;
second=i;
}
}
}
} for(int i=;i<n;i++){
cha=abs(a[i]-a[i-]);
if(maxx2<cha){
if(maxx3<=maxx1&&maxx3<=maxx2){
if(i==first||i==second){
continue;
}else{
maxx3=cha;
third=i;
}
}
}
} for(int i=;i<n-;i++){
cha2=abs(a[i+]-a[i-]);
if(maxx1<=cha2){
sum+=cha2;
}
if(maxx1>cha2){
if(i==first&&i+==second||i==second&&i+==first){
if(maxx3!=){
if(maxx3>=cha2){
sum+=maxx3;
}else{
sum+=cha2;
} }else{
sum+=cha2;
} }
if(i==first||i==first-){
if(cha2<=maxx2){
sum+=maxx2;
}else{
sum+=cha2;
}
}else{
sum+=maxx1;
} }
}
if(first==){
sum+=maxx2;
sum+=maxx1;
}
if(first==n-){
sum+=maxx2;
sum+=maxx1;
}
if(first!=&&first!=n-){
sum+=(*maxx1);
}
printf("%d\n",sum);
}
return ;
}