poj3744 Scout YYF I 【概率dp+矩阵优化】

20 篇文章 0 订阅
13 篇文章 0 订阅
解题思路:

很容易想到设dp[i]表示安全走到i的概率,则dp[i]=p dp[i-1]+(1-p)dp[i-2],然后有地雷的地方概率为0,最后dp[maxpos+1]就是答案;
但数据范围是[1, 100000000],显然过不了。

注意到地雷只有10个,所以可以把地图分为10段,每两个地雷之间的dp用矩阵优化就可以过了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
    if(c=='-')f=-1,c=getchar();
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

struct Matrix
{
    double a[3][3];
    friend inline Matrix operator * (const Matrix &A,const Matrix &B)
    {
        Matrix res;
        memset(res.a,0,sizeof(res.a));
        for(int i=1;i<=2;i++)
            for(int j=1;j<=2;j++)
                for(int k=1;k<=2;k++)
                    res.a[i][j]+=A.a[i][k]*B.a[k][j];
        return res;
    }

    void I()
    {
        memset(a,0,sizeof(a));
        a[1][1]=a[2][2]=1;
    }

    Matrix Pow(int y)
    {
        Matrix res,B=*this;
        res.I();
        for(;y;y>>=1,B=B*B)
            if(y&1)res=res*B;
        return res;
    }
}A,P;
int n,pos[105];
double p;

int main()
{
    //freopen("lx.in","r",stdin);
    while(scanf("%d%lf",&n,&p)!=EOF)
    {
        for(int i=1;i<=n;i++)
            pos[i]=getint();
        sort(pos+1,pos+n+1);
        if(pos[1]==1)
        {
            puts("0.0000000");
            continue;
        }

        P.a[1][1]=0,P.a[2][1]=1,P.a[1][2]=1-p,P.a[2][2]=p;
        A.a[1][1]=0,A.a[1][2]=1;
        pos[0]=1;
        for(int i=1;i<=n;i++)
        {
            int dis=pos[i]-pos[i-1]-1;
            A=A*P.Pow(dis);
            A.a[1][1]=A.a[1][2];
            A.a[1][2]=0;
        }
        A=A*P;
        printf("%0.7lf\n",A.a[1][2]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值