【推導】【數學期望】【冒泡排序】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem C. Earthquake

來源:互聯網
上載者:User

標籤:using   ble   struct   win   algorithm   ini   scan   ++i   int   

題意:兩地之間有n條不相交路徑,第i條路徑由a[i]座橋組成,每座橋有一個損壞機率,讓你確定一個對所有橋的檢測順序,使得檢測所需的總期望次數最小。

首先,顯然檢測的時候,是一條路徑一條路徑地檢測,跳躍地檢測沒有意義。考慮已經排好的某個路徑的順序,相鄰的兩條路徑j和j+1如果滿足:

(route[j].A+route[j].B)+(route[j+1].A+route[j+1].B)*(1.0-route[j].c)>
(route[j].A+route[j].B)*(1.0-route[j+1].c)+(route[j+1].A+route[j+1].B)

就交換它們的順序使得答案變得更優。

用類似冒泡的方法掃n次即可。

A是routej的全部橋良好的期望檢查次數,即所有橋好的機率之積乘以橋的數量。B是routej壞的情況下的期望檢測次數,相當於對每座橋損壞機率從大到小排序,然後對每個橋k,其前面k-1個橋全好,它壞的機率,乘上k,然後對這個值求和。c是所有橋好的機率之積,即這個路徑好的機率。

最後輸出答案的時候,對所有路徑求個其前面的所有路徑都壞的機率*(A+B)之和即可。

#include<cstdio>#include<algorithm>using namespace std;struct data{double A,B,c;}bridge[1005];int n,x[1005];int y[1005];bool cmp(const int &a,const int &b){return a>b;}int main(){//freopen("c.in","r",stdin);scanf("%d",&n);for(int i=1;i<=n;++i){scanf("%d",&x[i]);double nowhao=1.0;double huaiall=0;for(int j=1;j<=x[i];++j){scanf("%d",&y[j]);}sort(y+1,y+x[i]+1);for(int j=1;j<=x[i];++j){huaiall+=(double)j*nowhao*(1.0-(double)y[j]/1000.0);nowhao*=((double)y[j]/1000.0);}bridge[i].A=nowhao*(double)x[i];bridge[i].B=huaiall;bridge[i].c=nowhao;}for(int i=1;i<=n;++i){for(int j=1;j<n;++j){if((bridge[j].A+bridge[j].B)+(bridge[j+1].A+bridge[j+1].B)*(1.0-bridge[j].c)>(bridge[j].A+bridge[j].B)*(1.0-bridge[j+1].c)+(bridge[j+1].A+bridge[j+1].B)){swap(bridge[j],bridge[j+1]);}}}double ans=0;double now=1.0;double sum=0;for(int i=1;i<=n;++i){ans+=now*(bridge[i].A+bridge[i].B);now*=(1.0-bridge[i].c);}printf("%.10f\n",ans);return 0;}

【推導】【數學期望】【冒泡排序】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem C. Earthquake

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.