题目描述

You are trying to set up a straight line of dominos, standing on end, to be pushed over later for your entertainment. (Sure, it seems pointless to set something up only to knock it down again, but you have some strange hobbies) The tricky thing about setting dominos, however, is that if you make a mistake and knock one over as you place it, it will knock down any adjacent line of consecutive dominos on one side of it, partially ruining your work. 
For instance, if you've already placed dominos in the pattern DD__DxDDD_D, and you try placing a domino at position x, there is a chance it will fall and knock over the domino to the left or the three dominos to its right, forcing you to place them again.

This human error is somewhat unavoidable, but you can make the odds somewhat more favourable by using a domino-placing technique that leads to dominos falling in one direction more often than in the other.

Given the number of dominos you are trying to set up, and the probability that you'll knock over any individual domino either to the left or to the right while placing it, determine the average number of dominos you'll need to place before you finish. Assume that you're using an optimal placement strategy.

输入

Input will consist of up to 100 cases. Each case consists of one line of input. It will contain the number of dominos to place, n, 1 <= n <= 1000, followed by nonnegative values Pl and Pr, indicating the probability of any domino falling to the left or to the right when placed. You may assume 0 < Pl + Pr <= 0.5.

The last test case is followed by a line containing a single 0.

输出

For each case, output the expected number of dominos that will need to be placed before you finish, accurate to two digits after the decimal.
 
题意:你试图把一些多米诺骨牌排成直线,然后推倒它们。但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰倒,而你的工作也被部分的破坏了。 比如你已经把骨牌摆成了DD__DxDDD_D的形状,而想要在x这个位置再放一块骨牌。它可能会把左边的一块骨牌或右边的三块骨牌碰倒,而你将不得不重新摆放这些骨牌。 这种失误是无法避免的,但是你可以应用一种特殊的放骨牌方法来使骨牌更多的向一个方向倒下。 给出你要摆放的骨牌数目,以及放骨牌时它向左和向右倒的概率,计算你为完成任务摆放的骨牌数目的平均数。假设你使用了最佳的摆放策略。 输入将包含至多100个测试点,每个测试点占一行,包含需要摆放的骨牌数目n (1≤n≤1000),以及两个非负实数Pl, Pr,表示骨牌向左和向右倒的概率。保证1<Pl+Pr≤0.5。 最后一个测试点包含一个数0。对于每个测试点输出题目要求的数目,保留两位小数。
 
思路:我们想成功的放置1块骨牌所需要的次数的期望是多少呢? 1/(1-pl-pr) 也就是说我们平均每放1/(1-pl-pr)个能成功一个
dp[i]表示成功放置长度为i的骨牌所需要次数的期望
用dp的思想我们之前已经得到dp[1]~dp[n-1]
我们假设长度为n的骨牌最后一块放置在i位置上
那么dp[n]=min(E[i]+dp[i-1]+dp[n-i]) 1<=i<=n就是枚举最后放的i位置 其中E[i]表示第i块能放成功的期望
现在我们处理E[i],也就是说在i位置放置成功我们平均需要几次呢?
1.直接放成功 这样我只要成功1次
2.放下之后向左倒了,首先发生这件事的概率是pl,之前成功放置i-1块的期望是dp[i-1]次,我们把dp[i-1]毁掉的概率是pl,对于向左倒,我们毁掉的块数的期望是dp[i-1]*pl
3.放下之后向右边倒,同理我们这时毁掉的块数的期望是pr*dp[n-i]
我们在i位置放置成功所需次数的期望是1+pl*dp[i-1]+pr*dp[n-i]
那么成功这么多次,我们又需要放几次呢? 注意到我们平均每放1/(1-pl-pr)个能成功一个
那么显然是(1+pl*dp[i-1]+pr*dp[n-i])/(1-pl-pr)次啊
这个地方比较绕...我们可以反向考虑
假如dp[i-1]=10,pl=0.4,那么我们放置i的时候如果向左倒,我们平均的就要多放置4个
dp[n-i]=20,pr=0.1那么我们放置i的时候如果向右倒,我们平均的就要多放置2个
也就是说我们先成功放置4个这样他就不会向左倒,再成功放置2个他就不会向右倒,我们此时再放置一个他就一定不会倒
这样我们需要成功放置4+2+1=7次,每次成功都需要1/(1-pl-pr)次放置
那么期望E[i]显然是(1+pl*dp[i-1]+pr*dp[n-i])/(1-pl-pr)
然后就n^2出答案了,网上有优化成O(n)的...
 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
int n;
double l,r;
double dp[maxn];
double calc (int n,int x)
{
return (1.0+l*dp[x-]+r*dp[n-x])/(-l-r)+dp[x-]+dp[n-x];
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d",&n)){
if (n==) break;
scanf("%lf%lf",&l,&r);
dp[]=;
dp[]=1.0/(-r-l);
for (int i=;i<=n;++i){
dp[i]=calc(i,i);
for (int j=;j<=i;++j)
dp[i]=min(dp[i],calc(i,j));
}
printf("%.2f\n",dp[n]);
}
return ;
}

优化成O(n)

 #include<iostream>
#include<cstdio>
using namespace std;
double f[];
double pl,pr,ans;
int n;
inline double cal(int n,int i){
return (pl*f[i-]+pr*f[n-i]+)/(1.0-pl-pr)+f[i-]+f[n-i];//计算期望
}
int main(){
cin>>n;
int i,j,x=;
cin>>pl>>pr;
f[]=;f[]=1.0/(1.0-pl-pr);//初始化,0张牌期望为0,一张牌期望为既不左倒也不右倒的概率
for(i=;i<=n;i++){
f[i]=cal(i,x);
while(x<i&&cal(i,x+)<f[i]){f[i]=cal(i,x+);x++;}//更优的决策点一定在上一次决策点之后
}
printf("%.2lf",f[n]);//保留两位
return ;
}
 

UVA 10529 - Dumb Bones (概率dp)的更多相关文章

  1. UVA 10529 Dumb Bones 可能性dp 需求预期

    主题链接:点击打开链接 题意: 要在一条直线上摆多米诺骨牌. 输入n, l, r 要摆n张排,每次摆下去向左倒的概率是l, 向右倒的概率是r 能够採取最优策略.即能够中间放一段.然后左右两边放一段等, ...

  2. UVA 10529 - Dumb Bones(概率+区间dp)

    UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...

  3. [UVA 10529]Dumb Bones

    题面在这里 题意 放\(n\)个相连的骨牌,每次放的时候有\(pl\)的概率往左倒,有\(pr\)的概率往右倒,骨牌倒的时候可能会打翻左边相邻或者右边相邻的骨牌,并引起连锁反应直到最后一个骨牌旁边没有 ...

  4. #11 UVA 10529 Dumb Bones

    题意: 放一堆排,每放一张,有pa的概率让左边的全倒,有pb的概率让右边全倒 问在最优策略下,最少要放几张才能摆放出n张 1<=n<=1000 题解: 这题应该还是很经典的 首先是期望部分 ...

  5. Substring UVA - 11468 AC自动机+概率DP

    题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...

  6. UVA 11021 C - Tribles(概率DP)

    记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...

  7. UVa 11468 (AC自动机 概率DP) Substring

    将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...

  8. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  9. Dumb Bones UVA - 10529[多米诺重构]

    Dumb Bones UVA - 10529   来自绿书p176  题意 你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰 ...

随机推荐

  1. vue工程本地代码请求http发生跨域提示错误解决方法

    这个可以使用代理进行跨域,这样看来跨域的方法就有几种了,对于iframe中的用postmassage,对于vue工程中的跨域则使用代理模式. 代理模式配置如下: 在config文件夹下找到index. ...

  2. LOJ 2557 「CTSC2018」组合数问题 (46分)

    题目:https://loj.ac/problem/2557 第一个点可以暴搜. 第三个点无依赖关系,k=3,可以 DP .dp[ cr ][ i ][ j ] 表示前 cr 个任务.第一台机器最晚完 ...

  3. EasyUI 中的双击某行 并赋值给input事件

    项目是由mvc+easyUI开发,双击事件在下边.有注释写着呢 function DataList(supCode) { myDatagrid2.datagridId = "GridView ...

  4. 请教怎么查询ORACLE的历史操作记录!

    请问如何查询ORACLE的历史操作记录!!!!!我用的是linux oracle 11g r2,想查一下前几天的数据库的历史操作记录,例如对表的insert,delete,update等等的操作记录, ...

  5. 前端工具-让浏览器兼容ES6特性

    babel:将ES6翻译为ES5 问题: 可以处理import和export么? 不能,还是用Rollup或者webpack打包一下吧 可以处理Promise么? 不能,还是使用babel-plugi ...

  6. 解决“/bin/bash^M: bad interpreter: No such file or directory”

    在执行shell脚本时提示这样的错误主要是由于shell脚本文件是dos格式,即每一行结尾以\r\n来标识,而unix格式的文件行尾则以\n来标识.  查看脚本文件是dos格式还是unix格式的几种办 ...

  7. appium常见问题09_MAC打开uiautimatorviewer闪退怎么办?

    问题: 下载安装Android SDK后,并且已在.bash_profile文件中配置环境变量.但是在tools中打开定位工具uiautomatorviewer出现闪退. 解决: 首先检查环境变量配置 ...

  8. Cocos2d 之FlyBird开发---MainMenu类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. MainMenu类主要实现的是游戏主界面的布局,它相当于一个港口,有开向各处的航道,而游戏中的MainMenu则是有跳转到各个场景的一个集 ...

  9. ---搭建springMvc框架,希望对初学者有所参考

    Spring Mvc ==> Struts2   spring 无法替代   myBatis 工作量大 要自己操作sql语句 ==> hibernate   Spring Mvc 取代St ...

  10. sql优化 分字段统计查询

    select count(1) from pd_xxx_origin_xxx_data where create_time like '2019-02-23%' and source='20036' ...