目录
BZOJ4318 OSU!题解code
BZOJ4318 OSU!
题目传送门
题解
一道比较简单的期望\(Dp\)。我们记\(f[i]\)为到第\(i\)位时的期望分数,\(g[i]\)为期望长度,分析一下转移我们可以发现连续1的长度从\(x-1\)变成\(x\)时,贡献变化为\(f[i]=f[i-1]+(3*g[i−1]^2+3*g[i−1]+1)*a[i]\)。所以我们可以记\(g1[i]\)表示到第\(i\)位时的期望长度的平方,\(g2[i]\)为期望长度。每次转移的时候同时更新这两个值。
code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
const int maxn=1e5+500;
int n;
double x;
double f[maxn],g1[maxn],g2[maxn];
/*==================Define Area================*/
int main() {
read(n);
for(int i=1;i<=n;i++) {
scanf("%lf",&x);
g1[i]=(g1[i-1]+1)*x;
g2[i]=(g2[i-1]+2*g1[i-1]+1)*x;
f[i]=f[i-1]+(3*g2[i-1]+3*g1[i-1]+1)*x;
}
printf("%.1lf\n",f[n]);
return 0;
}
转载于:https://www.cnblogs.com/Apocrypha/p/9431782.html