一道鬧心的水題

來源:互聯網
上載者:User
G-WindTime Limit : 3000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)Total Submission(s) : 123   Accepted Submission(s) : 13Font: Times New Roman | Verdana | GeorgiaFont Size: ← →Problem DescriptionN棵樹和M個蘑菇排成一排. 每棵樹有自己的座標和高度, 每個蘑菇有自己的座標和權值. 一陣大風刮來, 每棵樹有Li%的機率往左邊倒, 有Ri%的機率往右邊倒, 有(1 – Li% - Ri%)的機率不倒. 如果一棵高度為H座標為X的樹往左邊倒下, 那麼座標在區間[X – H, X)內的蘑菇就會被砸到, 如果往右邊倒下, 那麼座標在區間(X, X + H]內的蘑菇就會被砸到. 求不被樹砸到的所有的蘑菇的權值之和的期望值。Input第一行包含2個整數N, M.
接下來N行, 每行4個整數X, H, L, R. 表示這棵樹的座標, 高度和往左右倒的機率. 即這棵樹有L%的機率往左倒, R%的機率往右倒.
接下來M行, 每行2個整數Y, W. 表示這個蘑菇的座標和權值.
[資料範圍]
對於20% 的資料, 1 <= N, M <= 1000;
對於100% 的資料, 1 <= N <= 10^5, 1 <= M <= 10^4, 樹和蘑菇的座標的絕對值不超過10^9, 樹的高度在[1, 10^9]範圍內, 每個蘑菇的權值在[1, 1000]範圍內, 每棵樹的L,R 滿足 0 <= L, R, L + R <= 100.Output輸出一個整數, 為最後答案. 保留4位小數.Sample Input
1 12 2 50 501 1
Sample Output
0.5000

機率論忘記的差不多了,而且對於期望方差標準差我是完全不記得了..期望,方差在這裡。關於這些背景知識 將來有時間的話 我一定要好好惡補一下。
這題的期望就是 E=∑xi*pi,這個公式了。開始寫了個純暴力,先枚舉蘑菇,再枚舉樹。大概10s左右。後面修改了一下,樹的區間排序(發現排序完全沒效果,以後要注意這種二維排序的局限性,排區間右端不能約束區間左端的變化,所以不能輕易break.),枚舉樹時用二分查,但是不能break。用時3.75s。詢問了下學妹,說純暴力... 先樹再蘑菇。想想也是,蘑菇是1維的,break起來很方便..寫了個..3.00s. 這就相當鬱悶了。再自己寫了個二分查,最佳化到了time:0.265000。嗯...算是完成任務了.
PS:標程寫得真爛!
#include<iostream>#include<cstdio>#include<algorithm>#include<time.h>using namespace std;struct tree{       int x,h;       double l,r;}t[111111];struct segment{       int l,r;       double p;}seg[222222];struct node{       int x,v;}no[11111];bool cmp( node a,node b ){     return a.x<b.x;}int bisearch( int x,int n ){    int l=0,r=n,m;    while( m=(l+r)/2,l<r )    {           if( no[m].x>x )               r=m-1;           else               l=m+1;    }    return m;}int main(){    freopen( "Wind7.in","r",stdin );    freopen( "date.out","w",stdout );    int n,m;    clock_t start,end;    double time;    start=clock();    while( scanf("%d %d",&n,&m)!=EOF )    {           for( int i=0;i<n;i++ )           {                scanf( "%d %d %lf %lf",&t[i].x,&t[i].h,&t[i].l,&t[i].r );                t[i].r/=100;                t[i].l/=100;           }           for( int i=0;i<m;i++ )                scanf( "%d %d",&no[i].x,&no[i].v );           sort( no,no+m,cmp );           double ans=0,p;/**           for( int i=0;i<m;i++ )           {                p=1;                int j=bisearch(no[i].x,2*n );                for( ;j<n*2;j++ )                {                     /**                     if( no[i].x>=t[j].x-t[j].h && no[i].x<=t[j].x )                         p*=1-t[j].l;                     else if( no[i].x>=t[j].x && no[i].x<=t[j].x+t[j].h )                         p*=1-t[j].r;                     /                     if( no[i].x>=seg[j].l && no[i].x<=seg[j].r )                         p*=1-seg[j].p;                     //if( no[i].x<seg[j].l )                     //    break;                }                ans+=p*no[i].v;           }*/           double a[11111];           for( int i=0;i<m;i++ )                a[i]=1;           for( int i=0;i<n;i++ )           {                int j=bisearch(t[i].x-t[i].h,m);                for( ;j<m;j++ )                {                     if( t[i].x-t[i].h<=no[j].x && no[j].x<t[i].x )                         a[j]*=1-t[i].l;                     if( t[i].x<no[j].x && no[j].x<=t[i].x+t[i].h )                         a[j]*=1-t[i].r;                     if( no[j].x>t[i].x+t[i].h  )                         break;                }           }                      for( int i=0;i<m;i++ )                ans+=a[i]*no[i].v;           printf( "%.4lf\n",ans );    }    end=clock();    time=(double)(end-start)/CLOCKS_PER_SEC;    printf( "time:%lf\n",time );    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.