bzoj 4318 OSU! —— 期望DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4318
期望DP,因为平方的期望不等于期望的平方,所以用公式递推;
第一次推错了囧,还是看这位的博客改过来的:https://blog.csdn.net/Clove_unique/article/details/62422100
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int const maxn=1e5+5; int n; double f[maxn],f2[maxn],f3[maxn],p[maxn],ans[maxn]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lf",&p[i]); f[i]=(f[i-1]+1)*p[i]; f2[i]=(f2[i-1]+2*f[i-1]+1)*p[i]; // f3[i]=(3*f2[i-1]+3*f[i-1]+1)*p[i]; // ans[i]=ans[i-1]+f3[i]*p[i];// ans[i]=ans[i-1]*(1-p[i])+(ans[i-1]+3*f2[i-1]+3*f[i-1]+1)*p[i];// } printf("%.1lf\n",ans[n]); return 0; }