欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

CodeForces - 236D Let‘s Play Osu!(概率dp)

发布时间:2024/4/11 55 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CodeForces - 236D Let‘s Play Osu!(概率dp) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出一个长度为 nnn 的字符串,第 iii 个位置有 pip_ipi 的概率为 OOO,有 1−pi1-p_i1pi 的概率为 XXX,本题的贡献指的是,连续 OOO 的个数的平方,问贡献的期望是多少

题目分析:如果按照正常思路不难想到一个 n2n^2n2dpdpdp,大概就是 dpi,jdp_{i,j}dpi,j 表示到了第 iii 个位置结束,选还是不选第 iii 个位置的期望,转移的话一层枚举位置,另一层枚举最后一个 OOO 的长度即可,不过很可惜这个题没法这样去想

考虑平方展开,假设 LLL 为原本的 OOO 的长度,当 L→L+1L \to L+1LL+1 后,贡献会增加:
(L+1)2−L2=(L2+2L+1)−L2=2L+1\begin{aligned} &(L+1)^2-L^2 \\ &=(L^2+2L+1)-L^2 \\ &=2L+1 \end{aligned} (L+1)2L2=(L2+2L+1)L2=2L+1

如此一来我们就可以线性维护一下期望长度,利用期望长度来计算贡献了

期望长度的线性转移话可以参考概率 dpdpdp 的入门题目:SDUT - 2623 The number of steps

假设到了第 i−1i-1i1 个位置的期望长度为 lenlenlen,那么到了第 iii 个位置时只有两种情况:

  • iii 个位置选择 OOO 与前面的 OOO 连起来,期望长度变为 len+1len+1len+1,概率为 pip_ipi
  • iii 个位置选择 XXX 与前面的 OOO 断开,期望长度为 000,概率为 1−pi1-p_i1pi
  • 而对于贡献来说,我们只需要统计一下每次增加的就可以了

    代码:

    // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; char s[N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;read(n);double cur=0,ans=0;for(int i=1;i<=n;i++){double p;cin>>p;ans+=(cur*2+1)*p;//到了第i-1个位置的期望长度为cur,如果当前位置为O的话贡献会增加2*cur+1cur=(cur+1)*p+0*(1-p);//计算到了当前位置时的期望长度}printf("%.10f\n",ans);return 0; } 超强干货来袭 云风专访:近40年码龄,通宵达旦的技术人生

    总结

    以上是生活随笔为你收集整理的CodeForces - 236D Let‘s Play Osu!(概率dp)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。