Maximum Random Walk
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 229 Accepted Submission(s): 128
Problem Description
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.
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
Source
Recommend
liuyiding
概率dp, 我们可以发现,dp[i][j][k]表示走了i步,到达了j,最远到过k, 我们发现,k是一定要大于j的,否则为0,因为我们要求的是走到最右端的期望,也就是要求走到最远右边每一个点的距离*它的概率,这样,我们可以先把概率算出来,我们可以得到壮态转移方程,如果,j>k那么就是 dp[i][j][k]=dp[i-1][j-1][k]*r+dp[i-1][j-1][k-1]*r+dp[i-1][j][k]*re;
否则,就是 dp[i][j][k]=dp[i-1][j+1][k]*l+dp[i-1][j][k]*re+dp[i-1][j-1][k]*r;这样,马上,就可以算出来了,注意这里因为有左右,所以要标记一个原点,这样就好处理了!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
double dp[225][225][225];
int fmax(int a,int b)
{
if(a>b)
return a;
return b;
}
int main()
{
int tcase,t,n,i,j,k;
double l,r,re;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%lf%lf",&t,&n,&l,&r);
re=1-l-r;
dp[0][110][110]=1;
for(i=1;i<=n;i++)
{
for(j=110-i;j<=i+110;j++)
{
for(k=fmax(j,110);k<=110+i;k++)
{
if(j==k)
dp[i][j][k]=dp[i-1][j-1][k]*r+dp[i-1][j-1][k-1]*r+dp[i-1][j][k]*re;
else
dp[i][j][k]=dp[i-1][j+1][k]*l+dp[i-1][j][k]*re+dp[i-1][j-1][k]*r;
//printf("%.4f\n",dp[i][j][k]);
}
}
}
double ans=0;
for(i=110-n;i<=110+n;i++)
{
for(k=110;k<=110+n;k++)
{
ans+=dp[n][i][k]*(k-110);
}
}
printf("%d %.4f\n",t,ans);
}
return 0;
}