G-Wind

算法:

计算期望值,算出每棵蘑菇的概率,再乘以其期望。

View Code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

struct pnode
{
 int h, x, l, r;
}p[100100];

struct qnode
{
 int x, w;
}q[11000];

int N, M;
int v[1000010];
int hash[1000100];
double lp[10100];
double rp[10100];
double lep[10100];
double rep[10100];

int main( )
{ 
  int cnt;
  while( scanf("%d%d",&N,&M) != EOF )
  {  
     cnt = 0;
     memset(hash,0,sizeof(hash));
     for( int i = 1; i <= M; i++)
     {
        lp[i] = 1;
        rp[i] = 1;
        lep[i] = 1;
        rep[i] = 1;     
     }
     for( int i = 1; i <= N; i++)
     {
        scanf("%d%d%d%d",&p[i].x,&p[i].h,&p[i].l,&p[i].r);
        v[cnt++] = p[i].x;
        v[cnt++] = p[i].h + p[i].x;
        v[cnt++] = p[i].x - p[i].h;
     }
     for( int i = 1; i <= M; i++)
     {
        scanf("%d%d",&q[i].x,&q[i].w);
        v[cnt++] = q[i].x;
     }
     sort(v, v + cnt);
     cnt = unique(v, v + cnt) - v ;
     for( int i = 1; i <= M; i++)
     {
        int x = lower_bound(v, v + cnt, q[i].x) - v;  
        hash[x] = i;
     }
     for( int i = 1; i <= N; i++)
     {
        int x = lower_bound(v, v + cnt, p[i].x) - v;
        int y = lower_bound(v, v + cnt, p[i].x + p[i].h) - v;
        int z = lower_bound(v, v + cnt, p[i].x - p[i].h) - v;
        for( int j = z; j <= x; j++)
        {
          if( hash[j] )
          {
             lp[hash[j]] *= (1-p[i].l/ 100.0);
            // double x = 100 - p[i].l - p[i].r;
            // if( x )
            // lep[hash[j]] *= x / 100; 
          }     
             
        }        
        for( int j = x; j <= y; j++)
        {
          if( hash[j] )
          {
             rp[hash[j]] *= (1-p[i].r / 100.0);
           //  double x = 100 - p[i].l - p[i].r;
           //  if( x )
            // rep[hash[j]] *= x / 100; 
          }     
             
        }        
          
     }
     double sp = 0, sum = 0;
     for( int i = 1; i <= M; i++)
     {       
        sp = lp[i] * rp[i];
        sum += sp * q[i].w;       
     }
     printf("%.4lf\n",sum);      
  }
  return 0;  
}

posted on 2012-08-21 16:35  more think, more gains  阅读(191)  评论(0编辑  收藏  举报

导航