HDU 5805 NanoApe Loves Sequence

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   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 

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

Input

The first line of the input contains an integer  , denoting the number of test cases. 

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

The second line of the input contains   integers  , denoting the elements of the sequence. 

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  .

Sample Input

1
4
1 2 3 4

Sample Output

6


给你n个数,现在要删一个数,删每个数的概率是一样的,
现在问你删一个值后的相邻数绝对值最大差的期望是多少,
因为担心精度误差,让你答案乘n

先把每个相邻数的差求出来 sort一下 要记住id
然后就一个一个删  求出当前数列的相邻数差的最大值 加就行了 

要注意当 n = 3时 只有两个差值  
所以当删除第二个数时就没有差值了
要特殊处理  直接 dmax = t  
就在这里WA了一发

还有就是不能用cin输入  就在这里T了一发。。。 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;

LL a[maxn];
struct Node
{
    LL d, id;
}dif[maxn];
int cmp(Node a, Node b)
{
    return a.d > b.d;
}
int main(void)
{
//	freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
    LL T, n, i, j, dmax, t, sum, dex;
    scanf("%I64d", &T);
    while (T--)
    {
        scanf("%I64d", &n);
        scanf("%I64d", &a[1]);
        for (i = 2; i <= n; i++){
            scanf("%I64d", &a[i]);
            t = a[i] - a[i-1];
            if (t < 0) t = -t;
            dif[i-1].d = t;
            dif[i-1].id = i-1;
        }
        sort(dif+1, dif+n, cmp);
        sum = 0;
        dex = 1;
        while (dif[dex].id == 1) dex++;
        sum += dif[dex].d;
        dex = 1;
        while (dif[dex].id == n-1) dex++;
        sum += dif[dex].d;
        for (i = 2; i <= n-1; i++){
            t = a[i-1] - a[i+1];
            if (t < 0) t = -t;
            dex = 1;
            while (dif[dex].id == i || dif[dex].id == i-1)
                dex++;
            if (dex == n) dmax = t;
            else dmax = max(t, dif[dex].d);
            sum += dmax;
        }
        printf("%I64d\n", sum);
    }

	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值