CodeForces - 236D Let‘s Play Osu!(概率dp)
題目鏈接:點(diǎn)擊查看
題目大意:給出一個(gè)長(zhǎng)度為 nnn 的字符串,第 iii 個(gè)位置有 pip_ipi? 的概率為 OOO,有 1?pi1-p_i1?pi? 的概率為 XXX,本題的貢獻(xiàn)指的是,連續(xù) OOO 的個(gè)數(shù)的平方,問(wèn)貢獻(xiàn)的期望是多少
題目分析:如果按照正常思路不難想到一個(gè) n2n^2n2 的 dpdpdp,大概就是 dpi,jdp_{i,j}dpi,j? 表示到了第 iii 個(gè)位置結(jié)束,選還是不選第 iii 個(gè)位置的期望,轉(zhuǎn)移的話一層枚舉位置,另一層枚舉最后一個(gè) OOO 的長(zhǎng)度即可,不過(guò)很可惜這個(gè)題沒(méi)法這樣去想
考慮平方展開(kāi),假設(shè) LLL 為原本的 OOO 的長(zhǎng)度,當(dāng) L→L+1L \to L+1L→L+1 后,貢獻(xiàn)會(huì)增加:
(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?
如此一來(lái)我們就可以線性維護(hù)一下期望長(zhǎng)度,利用期望長(zhǎng)度來(lái)計(jì)算貢獻(xiàn)了
期望長(zhǎng)度的線性轉(zhuǎn)移話可以參考概率 dpdpdp 的入門(mén)題目:SDUT - 2623 The number of steps
假設(shè)到了第 i?1i-1i?1 個(gè)位置的期望長(zhǎng)度為 lenlenlen,那么到了第 iii 個(gè)位置時(shí)只有兩種情況:
而對(duì)于貢獻(xiàn)來(lái)說(shuō),我們只需要統(tǒng)計(jì)一下每次增加的就可以了
代碼:
// #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個(gè)位置的期望長(zhǎng)度為cur,如果當(dāng)前位置為O的話貢獻(xiàn)會(huì)增加2*cur+1cur=(cur+1)*p+0*(1-p);//計(jì)算到了當(dāng)前位置時(shí)的期望長(zhǎng)度}printf("%.10f\n",ans);return 0; } 超強(qiáng)干貨來(lái)襲 云風(fēng)專(zhuān)訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的CodeForces - 236D Let‘s Play Osu!(概率dp)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CodeForces - 830C Ba
- 下一篇: CodeForces - 897E Wi