洛谷 1365 WJMZBMR打osu! / Easy
题目:https://www.luogu.org/problemnew/show/P1365
大水题。记录一下o的期望长度。
关键是(x+1)^2=x^2+2*x+1。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=3e6+5; int n; double dp[N],len[N]; char ch[N]; int main() { scanf("%d%s",&n,ch+1); for(int i=1;i<=n;i++) { if(ch[i]=='x'){ len[i]=0;dp[i]=dp[i-1]; } if(ch[i]=='o'){ len[i]=len[i-1]+1; dp[i]=dp[i-1]+2*len[i-1]+1; } if(ch[i]=='?'){ len[i]=(len[i-1]+1)/2; dp[i]=dp[i-1]+(2*len[i-1]+1)/2; } } printf("%.4lf",dp[n]); return 0; }