POJ 3744 Scout YYF I

概率$dp$,矩阵优化。

设$dp[i]$为到位置$i$存活的概率,那么如果位置$i$是雷区,$dp[i]=0$,否则$dp[i]=p*dp[i-1]+(1-p)*dp[i-2]$。求出最后一个雷区位置的后一个位置的$dp$值就是答案。长度较大,可以矩阵优化加速一下。输出%$lf$不让过,%$f$过了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
}

int n; double p,ans;
int a[20];

struct Matrix
{
    double A[4][4];
    int R, C;
    Matrix operator*(Matrix b);
};

Matrix X, Y, Z;

Matrix Matrix::operator*(Matrix b)
{
    Matrix c;
    memset(c.A, 0, sizeof(c.A));
    int i, j, k;
    for (i = 1; i <= R; i++)
        for (j = 1; j <= b.C; j++)
            for (k = 1; k <= C; k++)
                c.A[i][j] = c.A[i][j] + A[i][k] * b.A[k][j];
    c.R=R; c.C=b.C;
    return c;
}

void init(double p1,double p2)
{
    Z.A[1][1] = p1, Z.A[1][2] = p2; Z.R = 1; Z.C = 2;
    Y.A[1][1] = 1, Y.A[1][2] = 0, Y.A[2][1] = 0, Y.A[2][2] = 1; Y.R = 2; Y.C = 2;
    X.A[1][1] = 0, X.A[1][2] = 1-p, X.A[2][1] = 1, X.A[2][2] = p; X.R = 2; X.C = 2;
}

void work(int x)
{
    while (x)
    {
        if (x % 2 == 1) Y = Y*X;
        x = x >> 1;
        X = X*X;
    }
    Z = Z*Y;
}

int main()
{
    while(~scanf("%d%lf",&n,&p))
    {
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        if(a[1]==1) ans=0;
        else
        {
            bool flag=0;
            for(int i=1;i<n;i++) if(a[i]+1==a[i+1]) flag=1;

            if(flag==1) ans=0;
            else
            {
                double p1=0,p2=1; int pos=1, now=1;
                while(1)
                {
                    if(a[now]!=pos+1)
                    {
                        init(p1,p2);
                        work(a[now]-1-pos);
                        p1=Z.A[1][1]; p2=Z.A[1][2];
                        pos=a[now]-1;
                    }
                    double np1=0,np2=(1-p)*p2;
                    pos=pos+2; p1=np1; p2=np2;
                    now++;
                    if(now==n+1) break;
                }
                ans=p2;
            }
        }
        printf("%.7f\n",ans);
    }
    return 0;
}
时间: 2024-08-06 16:06:16

POJ 3744 Scout YYF I的相关文章

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 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,

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

题目链接:http://poj.org/problem?id=3744 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"

POJ 3744 Scout YYF I 矩阵快速幂优化--概率dp

点击打开链接 Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5416   Accepted: 1491 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 diffic

poj 3744 Scout YYF I(概率dp,矩阵优化)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5020   Accepted: 1355 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,

poj 3744 Scout YYF I 概率dp+矩阵乘法

分析: dis(k,v1,v2)函数求到当前位置概率为v1,到当前位置之前一步的概率为v2,前进k步到达位置的概率,然后矩阵加速. 代码: //poj 3744 //sep9 #include <iostream> #include <algorithm> using namespace std; int pos[12]; double p,mat[4][4]; double ans[4][4]; void mul1() { double c[4][4]; c[0][1]=c[1]

poj 3744 Scout YYF I(矩阵优化概率DP)

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5153   Accepted: 1404 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,

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 "mine road". This is a very long road, on which

poj 3744 Scout YYF I(矩阵优化概率)

http://poj.org/problem?id=3744 有n个雷,某人的起始位置在1,每次走一步的概率为p,走两步的概率是1-p,给出n个雷的位置,问最后成功走出雷区的概率. 放在高中应该是很简单的分步乘法求概率.即把每一个雷都没踩到的概率求出来,最后n个相乘就是顺利通过的概率.对于输入的n个位置进行分段1~num[1],num[1]+1~num[2]......每一段都只有一个雷num[i],每一段内踩不到雷的概率就是1-踩到num[i]雷的概率. 设dp[i]表示踩到第i个雷的概率,那

POJ 3744 Scout YYF I:概率dp

题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问你通过雷区的概率. 题解: 表示状态: dp[i] = probability moving to i 表示走到i的概率 找出答案: ans = dp[last_mine+1] last_mine:最右边一颗雷的位置 如何转移: dp[i] = dp[i-1] * p + dp[i-2] * (1-

poj 3744 Scout YYF I (概率DP+矩阵快速幂)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5062   Accepted: 1370 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