POJ-3744 Scout YYF I 概率DP
2013-08-05 17:47
357 查看
题目链接: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就不运算下去了。当然此题还可以用矩阵乘法来优化。
考虑概率收敛代码:
矩阵乘法优化:
简单的概率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=200010; const int INF=0x3f3f3f3f; const int MOD=10007,STA=8000010; const LL LNF=1LL<<55; const double EPS=1e-14; const double OO=1e30; const int dx[4]={-1,0,1,0}; const int dy[4]={0,1,0,-1}; const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //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[12],ok; while(~scanf("%d%lf",&n,&p)){ f1=0;f2=1; ok=1; for(i=0;i<n;i++)scanf("%d",&w[i]); sort(w,w+n); for(i=1,j=0;j<n;j++){ if(w[j]==i)ok=0; for(;i<w[j]-1 && sign(f2-f1);i++){ f3=f2*p+f1*(1-p); f1=f2,f2=f3; } i=w[j]+1; f2*=1-p; f1=0; } printf("%.7lf\n",ok?f2:0.0); } return 0; }
矩阵乘法优化:
//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=200010; const int INF=0x3f3f3f3f; const int MOD=10007,STA=8000010; const LL LNF=1LL<<55; const double EPS=1e-14; const double OO=1e30; const int dx[4]={-1,0,1,0}; const int dy[4]={0,1,0,-1}; const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //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=2; struct Matrix{ double ma[size][size]; Matrix friend operator * (const Matrix a,const Matrix b){ Matrix ret; mem(ret.ma,0); int i,j,k; for(i=0;i<size;i++) for(j=0;j<size;j++) for(k=0;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,0); for(i=0;i<size;i++) ret.ma[i][i]=1; for(;k;k>>=1){ if(k&1)ret=ret*A; A=A*A; } return ret; } int main(){ // freopen("in.txt","r",stdin); int i,j,w[12],ok; Matrix S,t; double F[2]; while(~scanf("%d%lf",&n,&p)){ S.ma[0][0]=0,S.ma[0][1]=1; S.ma[1][0]=1-p,S.ma[1][1]=p; F[0]=0,F[1]=1; ok=1; for(i=0;i<n;i++) scanf("%d",&w[i]); sort(w,w+n); for(i=0,j=1;i<n;i++){ if(w[i]==j){ok=0;break;} A=S; t=mutilpow(w[i]-j-1); F[1]=(t.ma[1][0]*F[0]+t.ma[1][1]*F[1])*(1-p); F[0]=0; j=w[i]+1; } printf("%.7lf\n",ok?F[1]:0.0); } return 0; }
相关文章推荐
- POJ 3744 Scout YYF I 概率DP matrix 一开始考虑错误TLE了
- POJ 3744 Scout YYF I(矩阵优化的概率DP)
- [概率dp 矩阵乘法] poj 3744 Scout YYF I
- POJ 3744 Scout YYF I(概率DP)
- POJ 3744 Scout YYF I (矩阵优化,分段计算的概率DP)
- poj 3744 Scout YYF I (矩阵快速幂 优化 概率dp)
- POJ 3744 Scout YYF I (矩阵优化的概率DP)
- POJ 3744|Scout YYF I|概率DP
- poj 3744 Scout YYF I(概率dp,矩阵优化)
- poj 3744 Scout YYF I(矩阵优化概率DP)
- POJ 3744 Scout YYF I 概率DP
- POJ 3744 Scout YYF I(概率DP+数学)
- POJ 3744 Scout YYF I(概率Dp)
- POJ 3744 Scout YYF I 详细题解(矩阵优化概率DP)水题
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
- POJ 3744 Scout YYF I 概率DP+矩阵优化
- POJ 3744:Scout YYF I 很好的一道概率题
- POJ 3744 Scout YYF I(概率+矩阵)
- POJ 3711 Scout YYF I 概率DP + 矩阵快速幂
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂