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 DescriptionConsider 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.
InputThe 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.
OutputFor 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
31 1 0.5 0.52 4 0.5 0.53 10 0.5 0.4
Sample Output
1 0.50002 1.18753 1.4965
SourceGreater New York 2012
Recommendliuyiding機率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;}