POJ-3744 Scout YYF I 概率DP

时间:2021-02-26 02:02:53

  题目链接:http://poj.org/problem?id=3744

  简单的概率DP,分段处理,遇到mine特殊处理。f[i]=f[i-1]*p+f[i-2]*(1-p),i!=w+1,w为mine点。这个概率显然是收敛的,可以转化为(f[i]-f[i-1])/(f[i-1]-f[i-2])=p-1。题目要求精度为1e-7,在分段求的时候我们完全可以控制进度,精度超出了1e-7就不运算下去了。当然此题还可以用矩阵乘法来优化。

  考虑概率收敛代码:

 //STATUS:C++_AC_0MS_164KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double f1,f2,f3;
double p;
int n; int main(){
// freopen("in.txt","r",stdin);
int i,j,w[],ok;
while(~scanf("%d%lf",&n,&p)){
f1=;f2=;
ok=;
for(i=;i<n;i++)scanf("%d",&w[i]);
sort(w,w+n);
for(i=,j=;j<n;j++){
if(w[j]==i)ok=;
for(;i<w[j]- && sign(f2-f1);i++){
f3=f2*p+f1*(-p);
f1=f2,f2=f3;
}
i=w[j]+;
f2*=-p;
f1=;
}
printf("%.7lf\n",ok?f2:0.0);
}
return ;
}

  

  矩阵乘法优化:

 //STATUS:C++_AC_16MS_164KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End double p;
int n; const int size=; struct Matrix{
double ma[size][size];
Matrix friend operator * (const Matrix a,const Matrix b){
Matrix ret;
mem(ret.ma,);
int i,j,k;
for(i=;i<size;i++)
for(j=;j<size;j++)
for(k=;k<size;k++)
ret.ma[i][j]=ret.ma[i][j]+a.ma[i][k]*b.ma[k][j];
return ret;
}
}A; Matrix mutilpow(int k)
{
int i,j;
Matrix ret;
mem(ret.ma,);
for(i=;i<size;i++)
ret.ma[i][i]=;
for(;k;k>>=){
if(k&)ret=ret*A;
A=A*A;
}
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,w[],ok;
Matrix S,t;
double F[];
while(~scanf("%d%lf",&n,&p)){
S.ma[][]=,S.ma[][]=;
S.ma[][]=-p,S.ma[][]=p;
F[]=,F[]=;
ok=;
for(i=;i<n;i++)
scanf("%d",&w[i]);
sort(w,w+n);
for(i=,j=;i<n;i++){
if(w[i]==j){ok=;break;}
A=S;
t=mutilpow(w[i]-j-);
F[]=(t.ma[][]*F[]+t.ma[][]*F[])*(-p);
F[]=;
j=w[i]+;
} printf("%.7lf\n",ok?F[]:0.0);
}
return ;
}