題目連接: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);
}
}