您的位置:首页 > 产品设计 > UI/UE

HDU 5805 NanoApe Loves Sequence

2016-08-07 20:09 253 查看
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.

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.

 

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
, L
, R
;
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
= 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU