HDU 5805 NanoApe Loves Sequence

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.
 

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.

1T10, 3n100000, 1Ai109
 

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 .
 

Sample Input
  
  
1 4 1 2 3 4
 

Sample Output

6

求n种情况的期望,直接把n种情况的答案求出来就好了,预处理一下从1到i的和从i到n的,然后循环一遍就好了。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m;
int a[N], L[N], R[N];
LL ans;

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        ans = 0;
        rep(i, 1, n) scanf("%d", &a[i]);
        L[1] = R[n] = 0;
        rep(i, 2, n) L[i] = max(L[i - 1], abs(a[i] - a[i - 1]));
        per(i, n - 1, 1) R[i] = max(R[i + 1], abs(a[i] - a[i + 1]));
        rep(i, 1, n)
        {
            if (i == 1) ans += R[2];
            else if (i == n) ans += L[n - 1];
            else ans += max(abs(a[i - 1] - a[i + 1]), max(L[i - 1], R[i + 1]));
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值