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
题意:给出一个队列,随机删除其中的一个数,求出最大相邻两个数的差的绝对值的和的期望。
听起来绕来绕去,其实就是枚举每个元素被删除,然后找出其中最大差值的绝对值,把他们全都加起来就好了。
不过我们要记住差值前三的值,因为删掉的可能是第一和第二共有的那个元素。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
long long a[100000 + 5];
long long d[100000 + 5];
const long long INF = 2e9;
int main()
{
int T;
int n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%I64d", &a[i]);
long long maxn_1 = -INF, maxn_2 = -INF, maxn_3 = -INF;///初始化,很重要,因为这个WA了好几遍
int num_1 = 0, num_2 = 0;
for (int i = 2; i <= n; ++i)
{
d[i] = a[i] - a[i-1];
if (abs(d[i]) > maxn_1)///第一大
{
maxn_2 = maxn_1;
maxn_1 = abs(d[i]);
num_2 = num_1;
num_1 = i;
}
else if (abs(d[i]) > maxn_2)///第二大
{
maxn_2 = abs(d[i]);
num_2 = i;
}
}
if (n > 3)
{
for (int i = 2; i <= n; ++i)///第三大
{
if (i != num_1 && i != num_2 && d[i] > maxn_3)
maxn_3 = d[i];
}
}
long long ans = 0;
if (d[2] == maxn_1) ans += maxn_2;///删除第一个或者最后一个时
else ans += maxn_1;
if (d[n] == maxn_1) ans += maxn_2;
else ans += maxn_1;
for (int i = 2; i < n; ++i)///枚举每个删除点
{
long long val = abs(a[i+1] - a[i-1]);
if (val >= maxn_1) ans += val;
else
{
if ((i == num_1 && i+1 == num_2) || (i == num_2 && i+1 == num_1))
ans += max(maxn_3, val);
else if (i == num_1 || i+1 == num_1)
ans += max(maxn_2, val);
else
ans += maxn_1;
}
}
printf("%I64d\n", ans);
}
return 0;
}