[BZOJ 4318] OSU!
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4318
[算法]
考虑如果已有x个1 , 那么 , 如果再增加一个1
将会对答案产生(x + 1) ^ 3 - x ^ 3 = 3x ^ 2 + 3x + 1的贡献
用Fi表示第i个数结尾 , x的期望 , Gi表示x ^ 2的期望 , Ansi表示前i个数获得分数的期望
显然 :
Fi = (Fi + 1)Pi
Gi = (Gi-1 + 2Fi-1 + 1)Pi
Ansi = Ansi-1 + (1 + 3Fi-1 + 3Gi-1)Pi
时间复杂度 : O(N)
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 100010 int n; double p[MAXN] , g[MAXN] , ans[MAXN] , f[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { scanf("%d" , &n); for (int i = 1; i <= n; i++) { scanf("%lf" , &p[i]); f[i] = (f[i - 1] + 1) * p[i]; g[i] = (g[i - 1] + 2 * f[i - 1] + 1) * p[i]; ans[i] = ans[i - 1] + (1 + 3 * f[i - 1] + 3 * g[i - 1]) * p[i]; } printf("%.1lf\n" , ans[n]); return 0; }