您的位置:首页 > 其它

hdu 4487 Maximum Random Walk

2013-08-12 19:55 691 查看
Consider the classic random walk: at each step, you have a 1/2 chance of taking a step to the left and a 1/2 chance of taking a step to the right. Your expected position after a period of time is zero; that is, the average over many such random walks is that
you end up where you started. A more interesting question is what is the expected rightmost position you will attain during the walk.

 

Input

The first line of input contains a single integer P, (1 <= P <= 15), which is the number of data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input consisting of four space-separated values. The first value is an integer K, which is the data set number. Next is an integer n, which is the number of steps to take (1 <= n <= 100). The final two are double precision
floating-point values L and R

which are the probabilities of taking a step left or right respectively at each step (0 <= L <= 1, 0 <= R <= 1, 0 <= L+R <= 1). Note: the probably of not taking a step would be 1-L-R.

 

Output

For each data set there is a single line of output. It contains the data set number, followed by a single space which is then followed by the expected (average) rightmost position you will obtain during the walk, as a double precision floating point value to
four decimal places.

 

Sample Input

3

1 1 0.5 0.5

2 4 0.5 0.5

3 10 0.5 0.4

 

Sample Output

1 0.5000

2 1.1875
3 1.4965

题目分析:根据概率求期望,这类题目最近见得也是比较多了,期望嘛,求出到达右边的每种可能情况的概率,然后乘以距离,最后所得的乘积和就是题目要求的期望了。

                    题目意思分心清楚了,由于每一步只与前一步有关,于是动态规划可以解决该问题。dp[i][j][k]表示走到第i步时,到达位置j,且所到达右边最远距离为k的概率,

                    于是,动态转移方程可以推导得出

                     当j==k 时

                                      dp[i][j][k]=dp[i-1][j][k]*re+dp[i-1][j-1][k-1]*r+dp[i-1][j-1][k]*r;

                     当j!=k 时

                                      dp[i][j][k]=dp[i-1][j][k]*re+dp[i-1][j-1][k]*r+dp[i-1][j+1][k]*l

# include<iostream>
# include<cstdio>
# include<cstring>
# define dd double
using namespace std;
dd dp[105][205][205];
int main()
{
int p;
scanf("%d",&p);
while (p--)
{
int t,n,i,j,k;
double l,r,re;
scanf("%d%d%lf%lf",&t,&n,&l,&r);
re=1-l-r;
//memset(dp,0,sizeof(dp)); 这里不能将dp初始化,否则会Memory Limit Exceeded
dp[0][100][100]=1;
for (i=1;i<=n;i++)
{
for (j=100-i;j<=100+i;j++)
{
if (j<100) k=100;
else k=j;
for (k;k<=100+i;k++)
if (j==k)
dp[i][j][k]=dp[i-1][j][k]*re+dp[i-1][j-1][k-1]*r+dp[i-1][j-1][k]*r;
else
dp[i][j][k]=dp[i-1][j][k]*re+dp[i-1][j-1][k]*r+dp[i-1][j+1][k]*l;
}
}
double ans=0;
for (i=101;i<=200;i++)
for (j=0;j<=200;j++)
ans+=dp
[j][i]*(i-100);
printf("%d %.4f\n",t,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp