Probability $dp$, matrix optimization.
Set $dp[i]$ to the probability of survival of the position $i$, then if the location $i$ is mined, $DP [i]=0$, otherwise $dp[i]=p*dp[i-1]+ (1-p) *dp[i-2]$. Finding the $dp$ value of the last location of a minefield is the answer. The length is large, can accelerate the matrix optimization. Output% $lf $ not allowed,% $f $ over.
#pragmaComment (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 namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-6;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} Template<classT>inlinevoidRead (T &x) { Charc =GetChar (); X=0; while(!isdigit (c)) C =GetChar (); while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();}}intNDoubleP,ans;inta[ -];structmatrix{Doublea[4][4]; intR, C; Matrixoperator*(Matrix b);}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix B) {matrix C; memset (C.A,0,sizeof(C.A)); intI, 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; returnC;}voidInitDoubleP1,Doublep2) {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;}voidWorkintx) { while(x) {if(x%2==1) Y = y*X; X= x >>1; X= x*y; } Z= z*Y;}intMain () { while(~SCANF ("%D%LF",&n,&p)) { for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Sort (a+1, A +1+N); if(a[1]==1) ans=0; Else { BOOLflag=0; for(intI=1; i<n;i++)if(a[i]+1==a[i+1]) flag=1; if(flag==1) ans=0; Else { Doublep1=0, p2=1;intpos=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; } Doublenp1=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;}
POJ 3744 Scout YYF I