HDU 4487 Maximum Random Walk 概率 dp

D - Maximum Random Walk

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice HDU
4487

Appoint description: 
System Crawler  (2016-05-03)

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.

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 

一个人在原点随机走n步 每次向左走的概率为L,向右走的概率为R 不走的概率是 1-R-L

问走的过程中向右走最远的期望

设dp【i,j,k】表示第i步走到j了此时曾经到过最远距离为k的概率

ACcode:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
double dp[2][202][202];
int main(){
    int loop;
    scanf("%d",&loop);
    while(loop--){
        int cnt,step,k;
        double l,r,s,ans;
        scanf("%d%d%lf%lf",&cnt,&step,&l,&r);
        s=1.0-l-r;
        memset(dp,0,sizeof(dp));
        dp[0][100][100]=1;
        for(int i=1;i<=step;++i)
            for(int j=100-i;j<=100+i;j++){
                for(k=j>100?j:100;k<=100+i;++k){
                    if(j==k)
                        dp[i&1][j][k]=dp[(i+1)&1][j][k]*s+dp[(i+1)&1][j-1][k-1]*r+dp[(i+1)&1][j-1][k]*r;
                    else
                        dp[i&1][j][k]=dp[(i+1)&1][j][k]*s+dp[(i+1)&1][j-1][k]*r+dp[(i+1)&1][j+1][k]*l;
                }
            }
        ans=0;
        for(int j=101;j<=101+step;++j)for(int i=100-step;i<=100+step;++i)ans+=dp[step&1][i][j]*(j-100);
        printf("%d %.4f\n",cnt,ans);
    }
    return 0;
}
时间: 2024-10-14 00:53:00

HDU 4487 Maximum Random Walk 概率 dp的相关文章

HDU 4487 Maximum Random Walk

Maximum Random Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 756    Accepted Submission(s): 419 三维dp,一维的话根本没有办法开展,二维的话没办法保存当前位置或者最远位置,所以只能用三维的. 看不懂滚动数组之类的操作,只能傻傻的写. 具体内容在代码里标注了,三重循环,从i,j,

HDU 4487 Maximum Random Walk(概率DP)

题目链接:点击打开链接 思路:概率DP, 用d[i][j][k]表示第i步, 走到j点, 走过的最大值为k的概率.  然后最后用概率乘以最右边走到的点就是期望, 期望相加就是答案. 细节参见代码: #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<

HDU 4865 Peter&#39;s Hobby --概率DP

题意:第i天的天气会一定概率地影响第i+1天的天气,也会一定概率地影响这一天的湿度.概率在表中给出.给出n天的湿度,推测概率最大的这n天的天气. 分析:这是引自机器学习中隐马尔科夫模型的入门模型,其实在这里直接DP就可以了 定义:dp[i][j]为第i天天气为j(0,1,2分别表示三个天气)的概率,path[i][j]记录路径,path[i][j] = k 意思是前一天天气为k时,这一天有最大的概率是天气j. 做一个三重循环,对于每天,枚举今天的天气,再在里面枚举昨天的天气,则有: dp[i][

HDU 4405 Aeroplane chess (概率DP求期望)

题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点需要步数的期望 其中有m个跳跃a,b表示走到a点可以直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点可以到走到i+1,i+2,i+3,i+4,i+5,i+6 点且每个点的概率都为1/6 所以dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])/6  + 1(步数加一). 而对于有跳跃的点直接为dp[a]=dp[b]; #include<stdio.h>

hdu 4418 Time travel (概率dp 细节好多)

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1366    Accepted Submission(s): 303 Problem Description Agent K is one of the greatest agents in a secret organization called Men in

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t

HDU5001 Walk(概率DP)

A - Walk Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5001 Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation loo

hdu 3076 ssworld VS DDD (概率dp)

///题意: /// A,B掷骰子,对于每一次点数大者胜,平为和,A先胜了m次A赢,B先胜了n次B赢. ///p1表示a赢,p2表示b赢,p=1-p1-p2表示平局 ///a赢得概率 比一次p1 两次p0*p1 三次 p0^2*p1,即A赢的概率为p1+p*p1+p^2*p1+...p^n*p1,n->无穷 ///即a_win=p1/(1-p);b_win=p2/(1-p); ///dp[i][j]表示a赢了j次,b赢了i次的概率 ///dp[i][j]=dp[i-1][j]*b_win+dp[

HDU 4050 wolf5x(动态规划-概率DP)

wolf5x Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 402    Accepted Submission(s): 248 Special Judge Problem Description There are n grids in a row. The coordinates of grids are numbered fro