HDU 5805 NanoApe Loves Sequence

来源:互联网 发布:淘宝摄影师需要助理嘛 编辑:程序博客网 时间:2024/06/06 12:56
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
141 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;}


0 0
原创粉丝点击