POJ 3744 Scout YYF I

poj - 3744 Scout YYF I Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "... 阅读详情
Scout YYF I
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4090 Accepted: 1050

Description

YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous "mine road". This is a very long road, on which there are numbers of mines. At first, YYF is at step one. For each step after that, YYF will walk one step with a probability of  p, or jump two step with a probality of 1- p. Here is the task, given the place of each mine, please calculate the probality that YYF can go through the "mine road" safely.

Input

The input contains many test cases ended with  EOF.
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

For each test case, output the probabilty in a single line with the precision to 7 digits after the decimal point.

Sample Input

1 0.5
2
2 0.5
2 4

Sample Output

0.5000000
0.2500000

Source


概率DP    根据乘法定理进行分段处理

这样每一段只有一个地雷。我们只要求得通过每一段的概率。乘法原理相乘就是答案。
对于每一段,通过该段的概率等于1-踩到该段终点的地雷的概率。
 
就比如第一段 1~x[1].  通过该段其实就相当于是到达x[1]+1点。那么p[x[1]+1]=1-p[x[1]].
但是这个前提是p[1]=1,即起点的概率等于1.对于后面的段我们也是一样的假设,这样就乘起来就是答案了。
 
对于每一段的概率的求法可以通过矩阵乘法快速求出来。

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

double p;
double num[2][2];

void fun(int x)
{
    int i,j,k;
    double cnt[2][2]={p,1.0-p,1.0,0.0},tmp[2][2];
    num[0][0]=num[1][1]=1.0;
    num[0][1]=num[1][0]=0.0;
    while(x>0)
    {
        if(x&1)
        {
            for(i=0;i<2;i++)
                for(j=0;j<2;j++)
                 {
                     tmp[i][j]=num[i][j];
                     num[i][j]=0.0;
                 }

            for(i=0;i<2;i++)
                for(j=0;j<2;j++)
                    for(k=0;k<2;k++)
                        num[i][j]+=tmp[i][k]*cnt[k][j];
        }
        for(i=0;i<2;i++)
            for(j=0;j<2;j++)
            {
                tmp[i][j]=cnt[i][j];
                cnt[i][j]=0.0;
            }

        for(i=0;i<2;i++)
            for(j=0;j<2;j++)
                for(k=0;k<2;k++)
                    cnt[i][j]+=tmp[i][k]*tmp[k][j];
        x>>=1;
    }
    return ;
}

int main()
{
    int N,i,cur,flag;
    int X[100]={-100};
    double d[10];
    double ans;

    while(scanf("%d%lf",&N,&p)==2)
    {
        for(i=1;i<=N;i++)
            scanf("%d",&X[i]);
        sort(X+1,X+1+N);
        if(X[1]==1)
        {
            printf("0.0000000\n");
            continue;
        }
        flag=0;
        for(i=1;i<N;i++)
            if(X[i]+1==X[i+1])
            {
                flag=1;
                printf("0.0000000\n");
                break;
            }
        if(flag)
            continue;
        ans=1.0;
        cur=1;
        for(i=1;i<=N;i++)
        {
            d[1]=1.0;   d[2]=p;
            if(X[i]-cur==1)
            {
                ans*=(1-d[2]);
                cur=X[i]+1;
                continue;
            }
            fun(X[i]-cur-1);
            d[3]=num[0][0]*d[2]+num[0][1]*d[1];
            ans*=(1-d[3]);
            cur=X[i]+1;
        }
        printf("%.7lf\n",ans);
    }
    return 0;
}



POJ3744】【Scout YYF I】 Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6162   Accepted: 1755 Description YYF is a couragous scout. Now he is on a dangerous mission wh 阅读详情

相关推荐

poj 3744 Scout YYF I

题目:poj 3744 Scout YYF I tag:概率dp 思路:对于当前的位置i,p的概率来自i-1,(1-p)的概率来自 i-2 的位置,但是有雷的位置,概率为0 设dp[i]表示走到i位置的概率,dp[0]=0,dp[1]=1,dp[i]=p*dp[i-1]+(1-p)*dp[i-2]  有雷的地方 dp[i]=0 由于范围比较大,但是雷的个数只有10个,所以讨论到达每一个雷的

shiyuan的专栏 1123

POJ3744Scout YYF I

Description YYF是一个英勇的侦查员。现在他正在执行打入到敌方内部的危险任务。在解决了一系列的险情后,YYF到达了敌方著名的"地雷路"起始点。这条路非常长,上面被精心排布了不少地雷。一开始,YYF站在1的位置。对于后面的路程,YYF有p的概率向前走一步,或者有1−p的概率向前跳两步。现在问题来了。非常喜欢坑队友的情报部得到了每个地雷的位置,但他们不准备告诉YYF,反而请你计算...

aijian7152的博客 233

poj3744 Scout YYF I

<br /><br />Scout YYF I<br />Time Limit:1000MS<br /> <br />Memory Limit:65536K<br />Total Submissions:3290<br /> <br />Accepted:797<br />Description<br />YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base

喵呜的Blog 2793

POJ 3744(Scout YYF I )

题意: 从1开始每次有p的概率往前跳一步,1-p的概率跳两步。给定n个点以及它们的坐标,若跳到这些点上则算失败,求安全经过这些点的概率。 分析:容易推出 dp[i] = dp[i-1]*p + dp[i-2]*(1-p) ; 把n个点的坐标从小到大排序,每两个点之间为一段,若要来到下一段必须从上一段跳到下一段的第一个点。又因为 n<=10 ,设所求最终概率为 ans , 则 ...

Joker & Liar的博客 282

Scout YYF I POJ - 3744

题意: 有一个无限长的路,路上布满地雷,初始在位置1,每次 p 的概率到下一格,(1- p)的概率到下二格 给出 p 以及地雷坐标a[i],问安全走过的概率是多少? 思路: 递推,用矩阵快速幂推,p[  a[i] +1 ]    推到   p[ a[i+1] -1  ]    ,于是p[ a[i+1] +1  ] =   p[ a[i+1] -1  ] *(1-p)  #include

a7f650ebd327889c的博客 271

poj #3744 Scout YYF I

题目描述:http://poj.org/problem?id=3744 很容易看出递推公式:dp[i] = p*dp[i-1] + (1-p)*dp[i-2],鉴于有雷的地方要特殊处理 看了这篇blog,才知道要用矩阵和快速幂 http://www.cnblogs.com/kuangbin/archive/2012/10/02/2710586.html 后来又看到讨论里有人说有公式,

ZzebraA的博客 457

Scout YYF I_poj3744

Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's famous...

weixin_30709809的博客 74

Scout YYF I POJ - 3744(概率+矩阵快速幂)

题目地址:http://poj.org/problem?id=3744 题意:路上有N个雷(踩雷之后你会牺牲),初始时你在第一单元,每次可以走一个单元或者两个,概率分别为p,1-p;路长最大为100000000,求你安全通过的概率。 思路: 第一步: 设dp[i]是安全通过第i单元的概率,那么初识时ddp[0]=0, p[1]=1,递推关系为:dp[i]=p*dp[i-1]+(i-p)*dp[i-...

呼吸 258

poj 3744 Scout YYF I (矩阵)

Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties, YYF is now at the start of enemy's...

dielucuan8830的博客 140

poj3744 Scout YYF I

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4023   Accepted: 1029 Description YYF is a couragous scout. Now he is on a dangerous mission whic

习惯与规则决定命运 813

POJ 3744 Scout YYF I 概率DP

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9564   Accepted: 2799 Description YYF is a couragous scout. Now he is on a dangerous mission wh

J. Cole 410
上一篇: POJ 1753 Flip Game
下一篇: POJ 2096 Collecting Bugs
Magic_____
博客等级 码龄14年 11粉丝 160原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值