HDU 5753 Permutation Bo(期望)

Problem Description

There are two sequences h1hn and c1cn . h1hn is a permutation of 1n . particularly, h0=hn+1=0 .

We define the expression condition is 1 when condition is True,is 0 when condition is False.

Define the function f(h)=i=1nici[hi>hi1 and hi>hi+1]

Bo have gotten the value of c1cn , and he wants to know the expected value of f(h) .

Input

This problem has multi test cases(no more than 12).

For each test case, the first line contains a non-negative integer n(1n1000) , second line contains n non-negative integer ci(0ci1000) .

Output

For each test cases print a decimal - the expectation of f(h) .

If the absolute error between your answer and the standard answer is no more than 104 , your solution will be accepted.

分析

不同位置的 hi 对最终结果的期望贡献不同

h1 hn 对期望的贡献值为 hi2
hi(i1,n) 对期望的贡献值为 hi3
n=1 的时候需要特判


官方题解说考虑期望的线性性TAT,根本没想到。
比赛的时候统计了 n=2345... 的各位贡献情况发现的规律。

代码

#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 1000 + 10;
const int MOD = 1e9 + 7;
int c[MAXN];
int main()
{
    for(int n;scanf("%d",&n) != EOF;)
    {
        double dig = 0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&c[i]);
            if(i == 1 || i == n)    dig += c[i] * 3.0;
            else    dig += c[i] * 2.0;
        }
        if(n == 1)    printf("%.4lf\n",c[1]*1.0);
        else if(n == 2)    printf("%.4lf\n",(c[1]+c[2]) / 2.0);
        else
            printf("%.4lf\n",dig / 6.0);    
    }
}

你可能感兴趣的:(期望)