Maximum Random Walk(概率dp)

题意:

走n步,给出每步向左走概率l,向右走概率r,留在原地的概率 1-l-r,求能达到的最远右边距离的期望。

分析;

开始按期望逆求的方式分析,但让求的就是右边界没法退,懵了一会,既然逆着不能求,就先正着求概率,再根据期望的定义来求,试试行吗,想了想状态,dp[i][j][k],表示走了i步当前位置是j最远右边界是k时的概率,因为可能位置是负的所以位置都加上n。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 110
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
double dp[N][2*N][N],l,r;
int main()
{
    int n,o,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%lf%lf",&o,&n,&l,&r);
        memset(dp,0,sizeof(dp));
        dp[0][n][n]=1;
        for(int i=0;i<n;++i){
            for(int j=0;j<=2*n;++j)
                for(int k=j;k<=2*n;++k)
                {
                    dp[i+1][j][k]+=dp[i][j][k]*(1-l-r);
                    dp[i+1][j-1][k]+=dp[i][j][k]*l;
                    if(j+1>k)
                        dp[i+1][j+1][k+1]+=dp[i][j][k]*r;
                    else
                        dp[i+1][j+1][k]+=dp[i][j][k]*r;
                }
        }
         //期望的定义
        double total=0.0;
        for(int j=0;j<=2*n;++j)
            for(int k=j;k<=2*n;++k)
            total+=dp[n][j][k]*(k-n);
        printf("%d %.4lf\n",o,total);
    }
return 0;
}   
时间: 2024-10-10 04:51:00

Maximum Random Walk(概率dp)的相关文章

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 ha

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<

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 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

Hdu 5001 Walk 概率dp

Walk Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=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 looks like a connected bid

HDU - 5001 Walk(概率dp+记忆化搜索)

Walk 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 to an ad

HDU 5001 Walk 求从任意点出发任意走不经过某个点的概率 概率dp 2014 ACM/ICPC Asia Regional Anshan Online

题意: 给定n个点m条边的无向图 问: 从任意点出发任意走d步,从不经过某个点的概率 dp[i][j]表示从不经过i点的前提下,走了d步到达j点的概率. #include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <vector> #include <algorithm> #include <set> using n

[概率dp] hdu 5001 Walk

题意:n个点(1~n),m条边,走k次,双向边,每次随机走一条路,起点也随机,问不走到各个点的概率是多少. 思路: 概率dp[i][j][k] 前i步 走到j 走不到k的概率. 那么状态转移就是 j能走到的点,传递所有dp[i][j][k]的值乘上概率. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"