UVA 10529 - Dumb Bones (概率dp)
题目描述
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.
输入
The last test case is followed by a line containing a single 0.
输出
#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)的更多相关文章
- UVA 10529 Dumb Bones 可能性dp 需求预期
主题链接:点击打开链接 题意: 要在一条直线上摆多米诺骨牌. 输入n, l, r 要摆n张排,每次摆下去向左倒的概率是l, 向右倒的概率是r 能够採取最优策略.即能够中间放一段.然后左右两边放一段等, ...
- UVA 10529 - Dumb Bones(概率+区间dp)
UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...
- [UVA 10529]Dumb Bones
题面在这里 题意 放\(n\)个相连的骨牌,每次放的时候有\(pl\)的概率往左倒,有\(pr\)的概率往右倒,骨牌倒的时候可能会打翻左边相邻或者右边相邻的骨牌,并引起连锁反应直到最后一个骨牌旁边没有 ...
- #11 UVA 10529 Dumb Bones
题意: 放一堆排,每放一张,有pa的概率让左边的全倒,有pb的概率让右边全倒 问在最优策略下,最少要放几张才能摆放出n张 1<=n<=1000 题解: 这题应该还是很经典的 首先是期望部分 ...
- Substring UVA - 11468 AC自动机+概率DP
题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...
- UVA 11021 C - Tribles(概率DP)
记忆化就可以搞定,比赛里都没做出来,真的是态度有问题啊... #include <iostream> #include<cstdio> #include<cstring& ...
- UVa 11468 (AC自动机 概率DP) Substring
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...
- uva 11468 AC自动机+概率DP
#include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...
- Dumb Bones UVA - 10529[多米诺重构]
Dumb Bones UVA - 10529 来自绿书p176 题意 你试图把一些多米诺骨牌排成直线,然后推倒它们.但是如果你在放骨牌的时候不小心把刚放的骨牌碰倒了,它就会把相临的一串骨牌全都碰 ...
随机推荐
- mock.js模拟生成假数据
mock使用方法很简单, 下面是简单的用法, 详细的用法可以看官方文档, 写的很清楚, 下面的代码直接拷贝到本地html文件, 双击打开即可生成你想要的数据 <!DOCTYPE html> ...
- SQL各种JOIN
JOIN(= INNER JOIN):返回匹配的结果,没有匹配则没结果: LEFT JOIN(= LEFT OUTER JOIN):返回匹配的与左表的所有数据: RIGHT JOIN(= RIGHT ...
- sublime常用基础插件合集
插件介绍 Package Control 功能:安装包管理简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能使用方法:快捷键 Ctrl+Shift+P,输入 install 选中In ...
- 课下选作Main dc
一.中后缀定义: 中缀表达式:我们平时写的数学表达式一般为中缀表达式,如"5+2(3(3-12+1))",直接拿中缀表达式直接让计算机计算表达式的结果并不能做到. 后缀表达式:把中 ...
- (转)教你分分钟搞定Docker私有仓库Registry
转:https://www.cnblogs.com/Javame/p/7389093.html 一.什么是Docker私有仓库Registry 官方的Docker hub是一个用于管理公共镜像的好地方 ...
- QT中用QStettings生成INI文件来记录QFileDialog::getOpenFileName上次的打开路径
QSettings setting("./Setting.ini", QSettings::IniFormat); //QSettings能记录一些程序中的信息,下次再打开时可以读 ...
- apm 应用性能管理(启动优化/ 掉帧卡顿/ 耗电/ 内存泄漏等)
APM 首先查看各个阶段耗时 : (环境变量设置 dyldPRINTSTATISTICS = 1选项,) 1. 启动优化 关键: 找到耗时的原因 t总 = t1 (premain) + t1(main ...
- Java并发:搞定线程池(上)
原文地址:https://www.nowcoder.com/discuss/152050?type=0&order=0&pos=6&page=0 本文是在原文的基础+理解,想要 ...
- PromQL
PromQL (Prometheus Query Language) 是 Prometheus 自己开发的数据查询 DSL 语言,语言表现力非常丰富,内置函数很多,在日常数据可视化以及rule 告警中 ...
- PAT甲级【2019年3月考题】——A1159 Structure_of_a_BinaryTree【30】
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...