UVA 10529-Dumb Bones(概率dp)

题意:

给出放一个多米诺骨牌,向左向右倒的概率,求要放好n个骨牌,需要放置的骨牌的期望次数。

分析:

用到区间dp的思想,如果一个位置的左面右面骨牌都已放好,考虑,放中间的情况,

dp[i]表示放好前i个骨牌,要放的期望次数,枚举1-i,每个点做中间点求对应的期望,取最小值。

dp[i]=min(L*dp[l]+R*dp[r]+1/(1.0-L-R));

#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 read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
double dp[1010],l,r;
int n;
void solve(){
    dp[0]=0;
    dp[1]=1.0/(1-l-r);
    for(int i=2;i<=n;++i){
        dp[i]=INF;
    for(int j=0;j<i;++j){
        double tmp=((1.0-r)*dp[j]+(1.0-l)*dp[i-j-1]+1)/(1.0-l-r);
    dp[i]=min(dp[i],tmp);
    }
    }
    printf("%.2lf\n",dp[n]);
}
int main()
{
    while(~scanf("%d",&n)){
            if(n==0)break;
        scanf("%lf%lf",&l,&r);
        solve();
    }
return 0;
}
时间: 2024-08-13 16:29:49

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

UVA 10529 Dumb Bones 概率dp 求期望

题目链接:点击打开链接 题意: 要在一条直线上摆多米诺骨牌. 输入n, l, r 要摆n张排,每次摆下去向左倒的概率是l, 向右倒的概率是r 可以采取最优策略,即可以中间放一段,然后左右两边放一段等,摆放顺序任意. 问:在最佳策略下要摆成n张牌的期望次数. 思路: 点击打开链接 #include <cstdio> #include <iostream> #include <cstring> #include <queue> #include <algo

UVA 10529 - Dumb Bones (概率dp)

题目描述 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

uva 10529 - Dumb Bones(概率+区间dp)

题目连接:uva 10529 - Dumb Bones 题目大意:给定n,表示要放n个骨牌,每次放下骨牌,有可能向左倒的概率为pl,向右倒的概率为pr,如果倒下,会将那一侧的骨牌全部推倒,可以选择位置先后放骨牌,问说一种放骨牌次数最少的期望是多少. 解题思路:dp[i]表示放i个骨牌需要的步数期望,维护一个最优放的位置,dp[i] = min\{ (从i-1到i的步数)} + (0到i-1的步数)} (从i-1到i的步数):dp[i?j?1]?pl+dp[j]?pr+11?pl?pr (0到i-

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

UVA 10529 - Dumb Bones 题目链接 题意:你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰倒,而你的工作也被部分的破坏了. 比如你已经把骨牌摆成了DD__DxDDD_D的形状,而想要在x这个位置再放一块骨牌.它可能会把左边的一块骨牌或右边的三块骨牌碰倒,而你将不得不重新摆放这些骨牌. 这种失误是无法避免的,但是你可以应用一种特殊的放骨牌方法来使骨牌更多的向一个方向倒下. 给出你要摆放的骨牌数目,以及放

#11 UVA 10529 Dumb Bones

题意: 放一堆排,每放一张,有pa的概率让左边的全倒,有pb的概率让右边全倒 问在最优策略下,最少要放几张才能摆放出n张 1<=n<=1000 题解: 这题应该还是很经典的 首先是期望部分 我们通过枚举最后一步,来分割序列 很容易知道中间的点应该要放1/(1-pa-pb)次 那么左边倒的次数就是pa/(1-pa-pb)次 这样dp方程就很简单了 dp[i]=min((dp[ls]*pa+dp[rs]*pb)/(1-pa-pb)+dp[ls]+dp[rs]) 发现朴素的是n^2的 性质1: 单峰

UVA 10529 Dumb Bones

#include <iostream> #include <stdio.h> #include <cstring> #define MAX 1010 using namespace std; double pl,pr; double data[MAX]; void Init() { memset(data, 0, sizeof(data)); } double dp(int n) { double temp; int l,r; if(n==0) return 0; if

UVA 1637 Double Patience 概率DP

Double Patience Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Double Patience is a single player game played with a standard 36-card deck. The cards are shuffled and laid down on a table in 9 pile

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

将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. 1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 const int maxnode = 500; 7 const int sigma_size = 64; 8 int idx[2

UVA10529 Dumb Bones

UVA - 10529 Dumb Bones 题意:给定n,表示要放n个骨牌,每次放下骨牌,有可能向左倒的概率为pl,向右倒的概率为pr,如果倒下,会将那一侧的骨牌全部推倒,可以选择位置先后放骨牌,问说一种放骨牌次数最少的期望是多少. /* 设dp[i]表示放置连续的i个期望的步数. 需要枚举放置的位置,即左边和右边有多少个,放置成功的期望步数为1/(1-pl-pr),如果放置失败了,那么就会是左边或右边的骨牌倒塌,此时重建的期望步数为dp[l]*pl+dp[r]*pr,所以可以得到转移方程:

uva 12723 概率dp

Dudu is a very starving possum. He currently stands in the first shelf of a fridge. This fridge iscomposed of N shelves, and each shelf has a number Qi (1 ≤ i ≤ N) of food. The top shelf, whereDudu is, is identified by the number 1, and the lowest is