概率DP求解例题
1,逆推状态:山东省赛2013年I题
Problem I: The number of steps
Description
Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms …). Now she stands at the top point(the first layer), and the KEY of this maze is in the lowest layer’s leftmost room. Known that each room can only access to its left room and lower left and lower right rooms .If a room doesn’t have its left room, the probability of going to the lower left room and lower right room are a and b (a + b = 1 ). If a room only has it’s left room, the probability of going to the room is 1. If a room has its lower left, lower right rooms and its left room, the probability of going to each room are c, d, e (c + d + e = 1). Now , Mary wants to know how many steps she needs to reach the KEY. Dear friend, can you tell Mary the expected number of steps required to reach the KEY?
Input
Output
Please calculate the expected number of steps required to reach the KEY room, there are 2 digits after the decimal point.
Sample Input
3
0.3 0.7
0.1 0.3 0.6
0
Sample Output
3.41
思路:二维DP,d[i][j]表示在i行第j个的期望,
然后逆推,点有三种情况:
1)只能靠左走(最后一行):d[i][j] = 1;
2)能靠左走,能靠下一行的左,右走:d[i][j] = d[i][j-1]*c+d[i+1][j-1]*d + d[i+1][j]*e;
3)只能向下一层的左右走:d[i][j] = d[i+1[j-1]*a+d[i+1][j]*b;
#include <bits/stdc++.h>
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
#define N 50 int main()
{
ios::sync_with_stdio(false);
int n;
while(cin>>n&&n)
{
double dp[N][N];
double a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
memset(dp,0.00,sizeof(dp));
for(int i=n; i; i--)///µÚiÐÐ
{
for(int j = n+-i; j<=n; j++)///µÚj¸ö
{
if(i==n&&j==(n+-i))
continue;
else if(i==n)
dp[i][j] = j -;
else if(j==(n+-i))
dp[i][j] = dp[i+][j-]*a+dp[i+][j]*b+;
else
dp[i][j] = dp[i+][j-]*c+dp[i+][j]*d+dp[i][j-]*e+;
}
}
cout<<fixed<<setprecision()<<dp[][n]<<endl;
}
return ;
}
二,先算概率,后算期望
Description
ICPC
(International Cardboard Producing Company) is in the business of
producing cardboard boxes. Recently the company organized a contest for
kids for the best design of a cardboard box and selected M winners. There are N
prizes for the winners, each one carefully packed in a cardboard box
(made by the ICPC, of course). The awarding process will be as follows:
- All the boxes with prizes will be stored in a separate room.
- The winners will enter the room, one at a time.
- Each winner selects one of the boxes.
- The selected box is opened by a representative of the organizing committee.
- If the box contains a prize, the winner takes it.
- If
the box is empty (because the same box has already been selected by one
or more previous winners), the winner will instead get a certificate
printed on a sheet of excellent cardboard (made by ICPC, of course). - Whether there is a prize or not, the box is re-sealed and returned to the room.
The management of the company would like to know how many prizes will
be given by the above process. It is assumed that each winner picks a
box at random and that all boxes are equally likely to be picked.
Compute the mathematical expectation of the number of prizes given (the
certificates are not counted as prizes, of course).
Input
).
Output
first and only line of the output file should contain a single real
number: the expected number of prizes given out. The answer is accepted
as correct if either the absolute or the relative error is less than or
equal to 10 -9.
Sample Input
sample input |
sample output |
5 7 |
3.951424 |
sample input |
sample output |
4 3 |
2.3125 |
思路:一维DP,d[i]表示第i个人拿到奖品的概率;
从第二个人开始往后推,d[i] = d[i-1]*d[i-1] + (1-d[i-1])*(d[i-1]-1/n);
前一个人拿到奖品 + 前一个人没有拿到奖品
最后for循环累加d[i];d[i] 虽然是概率,但是在计算的时候已经把它那一步的期望算进去了,最后累加起来其实就是期望了。
/*
SGU 495
*/
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int MAXN=;
double dp[MAXN];//dp[i]表示第i个人得到礼物的概率 int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
dp[]=;
for(int i=;i<=m;i++)
{//第i个人得到礼物的概率:假如第i-1个人没有得到礼物,那么i得到礼物的概率和i-1一样。
//假如第i-1个人得到了礼物,那么i得到礼物的概率是i-1得到礼物概率减去1/n
dp[i]=(-dp[i-])*dp[i-]+dp[i-]*(dp[i-]-1.0/n);
}
double ans=;
for(int i=;i<=m;i++)ans+=dp[i];
printf("%.10lf\n",ans);
}
return ;
}
概率DP求解例题的更多相关文章
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- 概率dp入门
概率DP主要用于求解期望.概率等题目. 转移方程有时候比较灵活. 一般求概率是正推,求期望是逆推.通过题目可以体会到这点. poj2096:Collecting Bugs #include <i ...
- HDU 4405:Aeroplane chess(概率DP入门)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Problem Description Hzz loves ...
- HDU 4405 【概率dp】
题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...
- [HDU 4089]Activation[概率DP]
题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...
- HDU 4089 Activation(概率DP)(转)
11年北京现场赛的题目.概率DP. 公式化简起来比较困难....而且就算结果做出来了,没有考虑特殊情况照样会WA到死的.... 去参加区域赛一定要考虑到各种情况. 像概率dp,公式推出来就很容易写 ...
- 动态规划——概率dp
所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
随机推荐
- Docker 下载镜像慢的问题解决方法
让你火箭般的速度下载docker镜像! 的冷漠度 百家号17-11-1713:09 因为有墙的原因所以在国内下载docker镜像的速度非常慢,有时候是几kb每秒,那个蛋疼的等待,真是谁等谁知道!下面我 ...
- gstreamer如何查看相关插件信息(src/sink)?
gstreamer及相关插件编译完成后,会输出gst-inspect可执行文件,相关信息如下: drwxrwxr-x yingc yingc 6月 : glib-/ drwxrwxr-x yingc ...
- intellij 自动导包
- 通过__block的作用深入研究block
block普通引用 默认情况下,在block中访问外部变量是通过复制一个变量来操作的,既可以读,但是写操作不对原变量生效,下面通过代码来举证 NSString *a = @"testa&qu ...
- mysqldump详解之--master-data
在前一篇文章中,有提到mysqldump的--single-transaction参数.另外还有个很重要,也是运维中经常用到的参数:--master-data,网上很多关于MySQL不停机备份的实现都 ...
- Mongodb系列- CRUD操作介绍
---恢复内容开始--- 一 Create 操作 在MongoDB中,插入操作的目标是一个集合. MongoDB中的所有写入操作在单个文档的层次上都是原子的. For examples, see In ...
- MySQL技术内幕读书笔记(七)——锁
锁 锁是数据库系统区分与文件系统的一个关键特性.为了保证数据一致性,必须有锁的介入.数据库系统使用锁是为了支持对共享资源进行并发访问,提供数据的完整性和一致性. lock与latch 使用命令 ...
- 抽奖活动 mark
).prizeName().remainingPrize().prizeRate().prizeName().remainingPrize().prizeRate().prizeName().rema ...
- Azure CentOS挂载磁盘
查看新增挂载磁盘 ls -l /dev/sd*  sudo fdisk /dev/sdc 依次输入:n,p,1,w  3.格式化分区 sudo mkfs -t ext4 /dev/sdc1  4 ...
- 怎么让Windows2012和Windows2008多用户同时远程
具体方法请参照百度经验:http://jingyan.baidu.com/article/cd4c2979f19765756e6e60ec.html.经过实践证明,是没有问题的.