CF 518 D. Ilya and Escalator 概率dp
来源:互联网 发布:教学过程最优化 编辑:程序博客网 时间:2024/05/16 03:15
链接
题意:
有n个人,每秒有p的概率有一个人进电梯,问t秒后电梯里的人数的期望。
解法:
因为有人数上限,所以要使用二维记录当前时间和人数。之后根据概率进行状态转移。
注意每个阶段概率和为1,编程上更新后继状态更简单。
代码:
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])#define mem(a,x) memset(a,x,sizeof a)#define ysk(x) (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn= 2000 ;int N,T;double p;double dp[maxn+3][maxn+3];int main(){ std::ios::sync_with_stdio(false); while(cin>>N>>p>>T) { mem(dp,0); dp[0][0]=1; for(int t=0;t<T;t++) { for(int n=0;n<N ;n++ ) { dp[t+1][n]+= (1-p)*dp[t][n]; dp[t+1][n+1]+= p*dp[t][n]; } dp[t+1][N]+=dp[t][N]; } double ans=0; for(int n=0;n<=N;n++) { ans+=n*dp[T][n]; } printf("%.6f\n",ans); } return 0;}
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])#define mem(a,x) memset(a,x,sizeof a)#define ysk(x) (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn= 2000 ;int N,T;double p;double dp[maxn+3][maxn+3];int main(){ std::ios::sync_with_stdio(false); while(cin>>N>>p>>T) { mem(dp[0],0); dp[0][0]=1; for(int t=1;t<=T;t++) { dp[t][0]=(1-p)*dp[t-1][0]; for(int n=1;n<N ;n++ ) { dp[t][n]= (1-p)*dp[t-1][n]+ p*dp[t-1][n-1]; } dp[t][N]=dp[t-1][N]+p*dp[t-1][N-1]; } double ans=0; for(int n=0;n<=N;n++) { ans+=n*dp[T][n]; } printf("%.6f\n",ans); } return 0;}
0 0
- CF 518 D. Ilya and Escalator 概率dp
- codeforces 518D D. Ilya and Escalator(概率dp)
- Codeforces 518D Ilya and Escalator (概率dp)
- Ilya and Escalator - CodeForces 518 D 概率dp
- CodeForces 518 D. Ilya and Escalator(概率DP)
- codeforces--518D--Ilya and Escalator--概率DP
- codeforces 518D. Ilya and Escalator 概率dp
- Codeforces 518D Ilya and Escalator【概率dp】
- 概率dp- Ilya and Escalator
- CF 518D(Ilya and Escalator-组合数太大,就直接拿去Dp)
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator (概率DP)
- Codeforces Round #293 (Div. 2)D. Ilya and Escalator(概率DP)
- Codeforces Round #293 (Div. 2)D.Ilya and Escalator——概率dp
- Codeforces Round #293 (Div. 2) -- D. Ilya and Escalator(概率DP)
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator(概率DP)
- codeforces 518D Ilya and Escalator
- Codeforces 518D Ilya and Escalator
- 【DP】 Codeforces Round #293 (Div. 2) D. Ilya and Escalator
- 一些面试问题总结
- 产品经理---- idea good idea
- 运营汇编2 两个文件如何去重
- Servlet~Listener详解
- NFA 有限自动机原理
- CF 518 D. Ilya and Escalator 概率dp
- C# MD5获取错误
- iOS摇一摇功能方法实现
- Lodash兼容IE6~IE8
- [POJ2823]Sliding Window(单调队列)
- BZOJ2951 [Poi2001]Goldmine
- UVA 11384 Help is needed for Dexter(构造题)
- HDU 3727 Jewel 主席树
- 说说PendingIntent的内部机制