您的位置:首页 > 大数据 > 人工智能

2016 Multi-University Training Contest 3 hdu 5753 Permutation Bo【打表+递推】

2016-07-26 18:34 507 查看

Permutation Bo

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
Special Judge


Problem Description

There are two sequences h1∼hn and c1∼cn. h1∼hn is a permutation of 1∼n. 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)=∑ni=1ci[hi>hi−1  and  hi>hi+1]

Bo have gotten the value of c1∼cn, 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(1≤n≤1000), second line contains n non-negative integer ci(0≤ci≤1000).

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 10−4, your solution will be accepted. 

Sample Input

4

3 2 4 5

5

3 5 99 32 12

Sample Output

6.000000

52.833333

 

题目大意:给你长度为n的ci序列,已知hi是一个全排列,求期望。

思路:

1、首先没有思路的时候,暴力打表,判断每一位上的ci都使用过多少次(累加过多少次)。

发现4的时候是:12 8 8 12

发现5的时候是:60 40 40 40 60

发现6的时候是:360 240 240 240 240 360.

2、这个时候突然开始发愁,我如果每个数字都乘上一个这么大的数,必然会有爆Long Long的存在啊,不必担心,因为期望的分母是情况数,也就是n的阶乘,直接令每一位使用次数除以n的阶乘之后再乘以Ci就好啦!

3、然后我们又发现:

12/24=1/2,8/24=1/3

60/120=1/2,40/120=1/3

360/720=1/2,240/720=1/3

4、那么整理其规律,ans=if(i==0||i==n-1)ans+1/2*ci,else ans+=1/2*ci【0<=i<n】

Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
double ans=0;
for(int i=0;i<n;i++)
{
int tmp;
scanf("%d",&tmp);
if(n==1)
{
ans+=tmp;
continue;
}
if(i==0||i==n-1)ans+=tmp*1.0/2*1.0;
else ans+=tmp*1.0/3*1.0;
}
printf("%.6lf\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu 5753 杭电 5753