POJ3744 Scout YYF I (矩阵优化的概率DP)
Input
Each test case contains two lines.
The First line of each test case is N (1 ≤ N ≤ 10) and p (0.25 ≤ p ≤ 0.75) seperated by a single blank, standing for the number of mines and the probability to walk one step.
The Second line of each test case is N integer standing for the place of N mines. Each integer is in the range of [1, 100000000].
Output
Sample Input
1 0.5
2
2 0.5
2 4
Sample Output
0.5000000
0.2500000 一位童子兵要穿过一条路,路上有些地方放着地雷。这位童子兵非常好玩,走路一蹦一跳的。
每次他在 i 位置有 p 的概率走一步到 i+1 ,或者 (1-p) 的概率跳一步到 i+2。童子兵初始在1位置,求他安全通过这条道路的概率。
以所在位置为状态,dp[i] 表示在位置 i 的安全的概率。
dp[i] = p * dp[i-1] + (1 - p) * dp[i-2]; // i 位置没有地雷
但是题目数据的范围是 10^8 这样dp的话会 TLE。
想想可以用矩阵快速幂优化。简单推出矩阵是
|p 1-p| * |dp[i] | = |dp[i+1]|
|1 0 | |dp[i-1]| |dp[i] |
而这时地雷位置是不满足这个矩阵的,因此我们得对地雷位置进行特判。而两个地雷中间的位置可以用快速幂优化。
假设 k 位置放有地雷,,我们可以得到 dp[k+1] = dp[k-1] * (1 - p);
对于炸弹位置为 a[i] 和 a[i+1] 之间的数,知道 dp[a[i]+1] 后可以推出
(视0位置有颗地雷,有地雷的位置的dp值为0)
于是我们可以对两个前后两个地雷之间用快速幂优化,并最终得到答案dp[max(a[i])+1];
转自:http://blog.csdn.net/xuelanghu407/article/details/47172759
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std; int n;double p;
int x[]; struct Node{double mat[][];};
Node mul(Node a,Node b)
{
Node res;
for(int i=;i<;i++)
for(int j=;j<;j++)
{
res.mat[i][j]=;
for(int k=;k<;k++) res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
}
return res;
}
Node pow_M(Node a,int n)
{
Node res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
Node temp=a;
while(n)
{
if(n&)res=mul(res,temp);
temp=mul(temp,temp);
n>>=;
}
return res;
}
int main()
{
while(~scanf("%d%lf",&n,&p))
{
for(int i=;i<n;i++)
scanf("%d",&x[i]);
sort(x,x+n);
double ans=;
Node tt;
tt.mat[][]=p;
tt.mat[][]=-p;
tt.mat[][]=;
tt.mat[][]=;
Node temp; temp=pow_M(tt,x[]-);
ans*=(-temp.mat[][]); for(int i=;i<n;i++)
{
if(x[i]==x[i-])continue;
temp=pow_M(tt,x[i]-x[i-]-);
ans*=(-temp.mat[][]);
}
printf("%.7f\n",ans);
}
}
POJ3744 Scout YYF I (矩阵优化的概率DP)的更多相关文章
- Scout YYF I POJ - 3744(概率dp)
Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into th ...
- poj3744 Scout YYF I[概率dp+矩阵优化]
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8598 Accepted: 2521 Descr ...
- [Poj3744]Scout YYF I (概率dp + 矩阵乘法)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9552 Accepted: 2793 Descr ...
- POJ3744 Scout YYF I 概率DP+矩阵快速幂
http://poj.org/problem?id=3744 题意:一条路,起点为1,有概率p走一步,概率1-p跳过一格(不走中间格的走两步),有n个点不能走,问到达终点(即最后一个坏点后)不踩坏点的 ...
- POJ-3744 Scout YYF I 概率DP
题目链接:http://poj.org/problem?id=3744 简单的概率DP,分段处理,遇到mine特殊处理.f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine ...
- POJ-3744 Scout YYF I (矩阵优化概率DP)
题目大意:有n颗地雷分布在一条直线上,有个人的起始位置在1,他每次前进1步的概率为p,前进两步的概率为1-p,问他不碰到地雷的概率. 题目分析:定义状态dp(i)表示到了dp(i)的概率,则状态转移方 ...
- 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)
题目: Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate int ...
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
题目链接 分析&&题意来自 : http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 题意: 在一条不满地雷的 ...
- [poj3744] Scout YYF I【概率dp 数学期望】
传送门:http://poj.org/problem?id=3744 令f(i)表示到i,安全的概率.则f(i) = f(i - 1) * p + f(i - 2) * (1 - p),若i位置有地雷 ...
随机推荐
- 插值(scipy.interpolate)
https://docs.scipy.org/doc/scipy/reference/interpolate.html#module-scipy.interpolate https://stackov ...
- SVN中的check out与export的区别
http://blog.csdn.net/zndxlxm/article/details/7763116 check out跟check in对应,export跟import对应. check out ...
- ucosii(2.89) 在Lpc1765移植中定时器的使用。
1,lpc1765的systicker register是24bit, cpu 频率64Mhz时候,注意不要设置systicker 的值超过24bit. 2, 使用timer 的callback函数, ...
- Maven01
1. Maven简单介绍 Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念. 作用:完成项目的相关操作,如:编译,构建,单元测试,安装,网站生成和基于Maven部 ...
- C程序(2)
- ubuntu 16.04 安装node.js 8.x
引自 https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04#how-to-in ...
- Spring框架中的aop操作之二 通过配置文件实现增强
aop表达式写法 配置文件代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- 基于IMD的包过滤防火墙原理与实现
一.前言二.IMD中间层技术介绍三.passthru例程分析四.部分演示代码五.驱动编译与安装六. 总结 一.前言 前段时间,在安全焦点上看到了TOo2y朋友写的<基于SPI的数据报过滤原理与实 ...
- perl学习二:简单变量
字符串变量:${}1.单引号:不进行变量替换,不进行转义,字符串可以跨行.2.双引号:变量替换(贪婪匹配原则).支持转义字符(转义字符可以另外看)3.反引号 字符串的特殊表示方法:qq(...) q( ...
- Processed foods make us fatter easily
From Business Insider Here's an experiment: sit alone in a hospital room for two weeks and eat nothi ...
