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 from x=1 to x=n. Someone starts from x=0. You can step forward with your left leg or right leg alternatively in turn.Namely,if you step forward with your left leg, then you must step with your
right leg next. As you can not jump , only one leg is allowed to use each step. Every step you take is in the range of [A,B], inclusively; namely, every step you take is at most B units and at least A units.

Before you start to move, the grids will be initialized randomly with 4 states(0,1,2,3), and p[i][j] means the probability of ith grid initialized with state j. After initialization, the state of the grids will not change.



State 0 means you can't step into the correspoding grid.

State 1 means you can just step into the grid with your left leg. 

State 2 means you can just step into the grid with your right leg.

State 3 means you can step into the grid with either of your legs,and the next step,you can use any legs; namely you don't need to follow the rules above.

If x>n, then the grid can be stepped in with arbitrary method.means you can step at the place after the nth grid.

For every step,you will choose the “step method” with the minimum step length. Namely, if you can take the step of S units and S+1 units, you will choose the step of S units.

Until you can't step in any grids in front of you,or you have been in a grid x>n, you will stop.

Can you calculate the expectation of the steps when you stop? 
 
Input
An integer T means the number of cases.T<=30

For each case,the first line is three integers n,A,B.

The next n lines,each line has 4 number p[i][0], p[i][1], p[i][2], p[i][3].

1 <= A <= B <= n<= 2000.

0 <= p[i][j] <= 1, p[i][0]+p[i][1]+p[i][2]+p[i][3] = 1.
 
Output
The expectation of the steps when you stop

you can assume that the relative epsilon is no more than 1e-6
 
Sample Input
9
2 1 1
0 0.5 0.5 0
0 0 1 0
2 1 1
0 0.5 0.5 0
0.5 0.5 0 0
2 1 2
0 0.5 0.5 0
0 0 1 0
2 1 2
0.2 0.3 0.4 0.1
0.15 0.2 0.25 0.4
3 1 10
0 0 0 1
0 0 0 1
0 0 0 1
3 1 1
0 0 0 1
0 0 0 1
0 0 0 1
3 2 2
0 0 0 1
0 0 0 1
0 0 0 1
3 3 3
0 0 0 1
0 0 0 1
0 0 0 1
3 1 2
0.0 0.3 0.6 0.1
0.1 0.2 0.3 0.4
0.5 0.4 0.1 0.0
 
Sample Output
2.00000000
1.50000000
2.50000000
2.46000000
4.00000000
4.00000000
2.00000000
2.00000000
2.80200000
 
Source
 
Recommend
lcy
 

题目大意:

这是一维的,一个人在0号格子,现在1~n号格子排成一排,上面有各种限制,一个人想从 0号格子走出n号格子,也就是走到 >n 处。

每个格子是4种状态的其中一种,并且没告诉你是哪种状态,只是告诉你概率,第i号格子4种状态的其中一种的概率记为p[i][0],p[i][1],p[i][2],p[i][3]。

0 表示这个格子既不能左腿也不能右腿踏进去。

1 表示这个格子可以左腿踏进去。

2 表示这个格子可以右腿踏进去。

3 表示这个格子既可以左腿也可以右腿踏进去。

这个人刚开始可以选择迈出左腿也可以迈出右腿,但是下次必须迈出不同的腿,除非你走在3这个状态下,下次又可以左右腿选一个,就和走路一样。

这个人一次迈出的步子的范围是[A,B],还有就是如果他迈出的步子可以是A+1或者A+2或者别的,他会选择最少的步数也就是 A+1

问你,这个人走的步数的数学期望,这个人走到 >n 处停止或者不能再走了也停止。

解题思路:

看到了数学期望,我就想到了概率DP,而这题的解法也就是概率DP,这个赛区已经看到了三道DP题目了。

(1)首先,用DP(pos,state) 记录这个状态下你还需要走的步数的数学期望

其中 ,pos记录你当前所在的格子,state记录你可以迈出的步子

0 表示你下一步不能走了,也就需要走的步数的数学期望0,是所以不会有这个状态,直接被剪纸剪掉了。

1 表示下一步要用左腿踏进去

2 表示下一步要用右腿踏进去

3 表示下一步既左腿也可以右腿踏进去

那么我么要求的就是DP(0,3)表示在0号格子,下一步既左腿也可以右腿踏进去,这个状态下你还需要走的步数的数学期望

(2)关于递推方程怎么求

DP(pos,state) =sum { p * ( DP(newpos,newstate) + 1 ) }

解释下我写的状态转移方程:

比如说,你当前在s位置,那么就枚举下一步走的位置 i, i>=s+A 且 i<=s+B

你走这个位置的概率,就是前面都不能走,也就是 s~i-1都同时为0状态,因为如果不是0状态你就得走,题目交代他会选择最少的步数。

所以得维护 s~i-1都同时为0状态的概率,也就是前缀相乘。

那么,现在讨论如果走了第 i 号位置

如果你的这一步要用右腿踏进去,下一步就得用左腿,如果这个位置的状态是3,你下个位置随意。

左腿同理。

解题代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std; const int maxn=2100;
double p[maxn][5],dp[maxn][5];
int a,b,n,vis[maxn][5],marked; void input(){
scanf("%d%d%d",&n,&a,&b);
for(int i=1;i<=n;i++){
for(int j=0;j<4;j++){
scanf("%lf",&p[i][j]);
}
}
} double DP(int k,int state){
if(k>n) return 0;
if(k==n) return 1;
if(vis[k][state]==marked) return dp[k][state];
double ans=0;
if(state==3){
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][2]*(DP(i,1)+1);
ans+=p0*p[i][1]*(DP(i,2)+1);
p0*=p[i][0];
}
}else if(state==2){
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][2]*(DP(i,1)+1);
p0*=(p[i][0]+p[i][1]);
}
}else{
double p0=1.0;
for(int i=k+a;i<=k+b;i++){
if(i>n){
ans+=p0*1.0;
break;
}
ans+=p0*p[i][3]*(DP(i,3)+1);
ans+=p0*p[i][1]*(DP(i,2)+1);
p0*=(p[i][0]+p[i][2]);
}
}
vis[k][state]=marked;
return dp[k][state]=ans;
} void solve(){
marked++;
printf("%.8lf\n",DP(0,3));
} int main(){
int T;
scanf("%d",&T);
while(T-- >0){
input();
solve();
}
return 0;
}

版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

HDU 4050 wolf5x(动态规划-概率DP)的更多相关文章

  1. HDU 4336 Card Collector(动态规划-概率DP)

    Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful card ...

  2. HDU 4089 Activation(概率DP)(转)

    11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况.   像概率dp,公式推出来就很容易写 ...

  3. 动态规划——概率dp

    所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...

  4. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  5. 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 t ...

  6. HDU 4050 wolf5x 概率dp 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4050 题意: 现在主角站在0处,需要到达大于n的位置 主角要进入的格子有三种状态: 0. 不能进入 1. 能进入 ...

  7. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  8. HDU 4576 Robot (概率DP)

    暴力DP求解太卡时间了...........写挫一点就跪了 //hdu robot #include <cstdio> #include <iostream> #include ...

  9. 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元

    http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...

随机推荐

  1. .net winform程序下使用firefox作为Web浏览器

    在winform程序中,要在程序中展示一个web页面,最常用的就是.net自带的webbrowser,但是大家都知道它是IE,也知道IE是有多么强(er)大(bi).而且微软已经宣布了IE的死亡... ...

  2. SQL Server 诊断查询-(1)

    Query #1 is Version Info. SQL and OS Version information for current instance SELECT @@SERVERNAME AS ...

  3. MVC中Action之间传值

    一  MVCAction之间的传值 之前一直觉得关于MVC里面的传值,只能从<视图—>Action>,和<Actoin->视图>但是今天在项目里面需要实现将几个视图 ...

  4. 2015 Multi-University Training Contest 1 - 1002 Assignment

    Assignment Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Mean: 给你一个数列和一个k,求连续区间的极值之 ...

  5. 一个端口操作类PortHelper

    此操作类主要使用了系统命令行来实现的,详细的命令可参照微软文档<如何使用"netsh advfirewall firewall"上下文而非“netsh firewall”上下 ...

  6. csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法

    https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...

  7. TCMalloc优化MySQL、Nginx内存管理

    TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员. 与标准的glibc库的Malloc相比,TCMalloc库在内存 ...

  8. android onNewIntent

    在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...

  9. 2015年第11本:代码整洁之道Clean Code

    前一段时间一直在看英文小说,在读到<Before I fall>这本书时,读了40%多实在看不下去了,受不了美国人啰啰嗦嗦的写作风格,还是读IT专业书吧. 从5月9日开始看<代码整洁 ...

  10. R语言学习笔记:小试R环境

    买了三本R语言的书,同时使用来学习R语言,粗略翻下来感觉第一本最好: <R语言编程艺术>The Art of R Programming <R语言初学者使用>A Beginne ...