CodeForces - 236D Let‘s Play Osu!(概率dp)
題目鏈接:點擊查看
題目大意:給出一個長度為 nnn 的字符串,第 iii 個位置有 pip_ipi? 的概率為 OOO,有 1?pi1-p_i1?pi? 的概率為 XXX,本題的貢獻指的是,連續 OOO 的個數的平方,問貢獻的期望是多少
題目分析:如果按照正常思路不難想到一個 n2n^2n2 的 dpdpdp,大概就是 dpi,jdp_{i,j}dpi,j? 表示到了第 iii 個位置結束,選還是不選第 iii 個位置的期望,轉移的話一層枚舉位置,另一層枚舉最后一個 OOO 的長度即可,不過很可惜這個題沒法這樣去想
考慮平方展開,假設 LLL 為原本的 OOO 的長度,當 L→L+1L \to L+1L→L+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)2?L2=(L2+2L+1)?L2=2L+1?
如此一來我們就可以線性維護一下期望長度,利用期望長度來計算貢獻了
期望長度的線性轉移話可以參考概率 dpdpdp 的入門題目:SDUT - 2623 The number of steps
假設到了第 i?1i-1i?1 個位置的期望長度為 lenlenlen,那么到了第 iii 個位置時只有兩種情況:
而對于貢獻來說,我們只需要統計一下每次增加的就可以了
代碼:
// #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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 830C Ba
- 下一篇: CodeForces - 897E Wi