HDU5753 Permutation Bo

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5753


【题意】给定n个数c1,c2,....cn。h1,h2,......hn是1-n数的组合。h0=h(n+1)=0。f(n)=∑ ci*[hi>h(i-1) and hi>h(i+1)]。[condition]在condition是true时为1,false时为0。求f(n)的期望。


【分析】头尾有都2种可能,其中一种是true,贡献为1/2。中间都有6种可能,其中2种可能是true,贡献为1/3。扫描求一下最后结果即可。


【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int num[1010];
int main(){
    int n;
    while(cin>>n){
        for(int i=0;i<n;++i)
            scanf(" %d",&num[i]);
        double ans=0,tmp1=1.0/2,tmp2=1.0/3;
        ans+=num[0]*tmp1+num[n-1]*tmp1;
        for(int i=1;i<n-1;++i)
            ans+=num[i]*tmp2;
        printf("%.06lf\n",ans);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章