LUOGU P1654 OSU! (概率期望)
解题思路
首先考虑对于一个点来说,如果这个点是1的话,那么对于答案来说 $(ans+1)^3=ans^3+3*ans^2+3*ans+1$,这对于上一个答案来说其实贡献了 $3*ans^2+3*ans+1$,那么只需要维护一个 $ans^2$与 $ans$的期望,然后转移到$ans$就行了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 100005; inline int rd(){ int x=0,f=1;char ch=getchar(); while(!isdigit(ch)) {f=ch=='-'?0:1;ch=getchar();} while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return f?x:-x; } int n; double f[MAXN],a1[MAXN],a2[MAXN],p[MAXN]; int main(){ cin>>n; for(int i=1;i<=n;i++) scanf("%lf",&p[i]); for(int i=1;i<=n;i++){ a1[i]=(a1[i-1]+1)*p[i]; a2[i]=(a2[i-1]+2*a1[i-1]+1)*p[i]; f[i]=f[i-1]+(3*a1[i-1]+3*a2[i-1]+1)*p[i]; } printf("%.1lf",f[n]); return 0; }