POJ 3744 Scout YYF I [概率DP]

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.

题意:

一条路上每个点标号为1,2,3....,你一开始站在1往正方向出发, 有P的概率走到下一格,(1-P)的概率走到下一格的下一格,现在路上可能会有一些地雷,给出N个地雷的坐标,问你不踩地雷走过这条路的概率。

范围:

地雷数小于等于10,坐标小于10忆

解法:

OXOOOOX

若O表示可以走的,X表示有地雷,那么走过这段路可以分成三个部分

第一部分:坐标1->坐标3 这一阶段达成的概率是1-p

第二部分:坐标3->坐标6

第三部分:坐标6->坐标8 这一阶段达成的概率是1-p

可以发现,只要求出第二部分的概率即可,这一部分的概率可以DP

记F[i]为走过长度为i的没有地雷的路程的概率,那么公式为f[i]=f[i-1]*p+f[i-2]*(1.0-p);

但是因为I可能很大,需要考虑对它进一步分析,最后发现i趋于无穷时,F[I]是有极限的,实际过程中这个极限很快就会达到。

所以处理出大约前1000个F[I],大于1000的取F[1000],然后按阶段处理即可。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int n,a[1111];
double p,f[1111];
int main(){
    while(scanf("%d%lf",&n,&p)!=EOF){
        mem(f,0);
        f[1]=1.0;
        rep(i,2,1000){
           f[i]=f[i-1]*p+f[i-2]*(1.0-p);
        }
        double ans=1.0;
        rep(i,1,n)scanff(a[i]);
        sort(a+1,a+1+n);
        int an=unique(a+1,a+1+n)-a-1;
        a[0]=0;
        rep(i,1,an){
            int x=a[i];
            int dis=min(a[i]-a[i-1]-1,1000);
            ans=ans*f[dis]*(1.0-p);
        }
        printf("%.7f\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值