Problem Description
NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entrance Examination!
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.
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.
Input
The first line of the input contains an integer
T
, 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,A2,...,An , denoting the elements of the sequence.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
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,A2,...,An , denoting the elements of the sequence.
1≤T≤10, 3≤n≤100000, 1≤Ai≤109
Output
For 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 .
In order to prevent using float number, you should print the answer multiplied by n .
Sample Input
1 4 1 2 3 4
Sample Output
6
PS:题意:任意去除一个数,找到每次两两差值绝对值的最大值之和
思路:找掐头去尾后差值最大值且记录差值并排序,相加后再找到去除任意一个数后的差值和未去除数差值中最大值比较,选取最大值相加
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[100020],b[100020],p[100020];
int abs(int a)
{
return a>0?a:-a;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
long long sum=0,maxx=0,num;
int k=1;
scanf("%d",&n);
scanf("%d",&a[0]);
scanf("%d",&a[1]);
int w=a[1]-a[0];
b[0]=w;
p[0]=w;
for(int i=2; i<n; i++)//找掐头或去尾后最大值去相加
{
scanf("%d",&a[i]);
b[k]=abs(a[i]-a[i-1]);
p[k]=b[k];
if(i==n-1)
{
if(maxx<b[k])
{
maxx=b[k];
}
if(w>maxx)
{
sum=maxx;
sum+=w;
maxx=b[k];
}
else
{
if(maxx<b[k])
{
sum=maxx;
sum+=b[k];
maxx=b[k];
}
else
{
sum=maxx;
sum*=2;
}
}
}
else
{
if(maxx<b[k])
{
maxx=b[k];
}
}
k++;
}
sort(p,p+k);//记录差值
// printf("%d %d %d %d %lld\n",p[0],p[1],p[2],p[3],sum);
for(int i=2; i<n; i++)
{
num=abs(a[i]-a[i-2]);
if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])!=p[k-2]||abs(a[i-1]-a[i-2])!=p[k-2]&&abs(a[i]-a[i-1])==p[k-1])
{
if(num<p[k-2])//若去除的数牵扯到最大值就加第二大值
sum+=p[k-2];
else
sum+=num;
}
else if(abs(a[i-1]-a[i-2])==p[k-1]&&abs(a[i]-a[i-1])==p[k-2]||abs(a[i-1]-a[i-2])==p[k-2]&&abs(a[i]-a[i-1])==p[k-1])
{
if(num<p[k-3])//若去除的数牵扯到最大值和第二大值就加第三大值
sum+=p[k-3];
else
sum+=num;
}
else
{ if(num<p[k-1])
sum+=p[k-1];
else
sum+=num;
}
//printf("%lld\n",sum);
}
printf("%I64d\n",sum);
}
}