Codeforces 513C Second price auction 数学公式求期望

题目大意:

就是n个公司(n <= 5个)分别自己出价, 每个公司出的价格随即在[Li, Ri]之间的任意一个整数(1 <= Li <= Ri <= 10000), 问随机出来的5个数中第二大的数的期望是多少


大致思路:

纯数学推理做的...有点厉害的样子...

数学公式见代码注释


代码如下:

Result  :  Accepted     Memory  :  4 KB     Time  :  15 ms

/*
 * Author: Gatevin
 * Created Time:  2015/2/26 10:22:50
 * File Name: poi~.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

/*
 * E = ∑b*P(b)
 * P(b)表示第二大的数是b的概率
 * P(b)的计算公式如下, 参考Codeforces第三种解法的计算公式(官方那个我看的时候有点问题)
 * (这里用II表示连乘, ∑表示累和)
 * P(b) = P(max = b, second = b) + P(max > b, second = b)
 * P(max = b, second = b) = P(max <= b) - P(max < b) - P(max = b, second < b)
 *                        = IIP(ri <= b) - IIP(ri < b) - ∑P(ri = b)*IIP(rj < b) (j != i)
 * P(max > b, second = b) = ∑P(ri > b)*(IIP(rj <= b) - IIP(rj < b)) (j != i)
 */

int n, L[6], R[6];

double Less(int j, int i)//return P(rj <= i)
{
    if(i < L[j]) return 0;
    if(i >= R[j]) return 1.;
    else return (i - L[j] + 1.)/(R[j] - L[j] + 1.);
}

double equal(int j, int i)//return P(rj == i)
{
    if(i < L[j] || i > R[j]) return 0;
    else return 1./(R[j] - L[j] + 1.);
}

double more(int j, int i)//return P(rj > i)
{
    if(i >= R[j]) return 0;
    if(i < L[j]) return 1.;
    return (R[j] - i)/(R[j] - L[j] + 1.);
}

int main()
{
    scanf("%d", &n);
    int ml = 10010, mr = 0;
    double ans = 0;
    for(int i = 1; i <= n; i++)
    {
        scanf("%d %d", L + i, R + i);
        ml = min(ml, L[i]);
        mr = max(mr, R[i]);
    }
    for(int i = ml; i <= mr; i++)
    {
        double p1 = 0;
        double p11 = 1, p12 = 1, p13 = 0;
        for(int j = 1; j <= n; j++)//这些O(n*n)的部分其实可以预处理降低复杂度
        {
            p11 *= Less(j, i), p12 *= Less(j, i - 1);//IIP(ri <= b) 和 IIP(ri < b)
            double tmp = equal(j, i);
            for(int k = 1; k <= n; k++)
                if(k != j)
                    tmp *= Less(k, i - 1);
            p13 += tmp;// ∑P(ri = b)*IIP(rj < b)
        }
        p1 = p11 - p12 - p13;//P(max = b, second = b)
        double p2 = 0;
        for(int j = 1; j <= n; j++)
        {
            double tmp1 = more(j, i);
            double tmp2 = 1., tmp3 = 1.;
            for(int k = 1; k <= n; k++)
                if(k != j)
                    tmp2 *= Less(k, i), tmp3 *= Less(k, i - 1);
            p2 += tmp1*(tmp2 - tmp3);//P(max > b, second = b) = ∑P(ri > b)*(IIP(rj <= b) - IIP(rj < b))
        }
        ans += (p1 + p2)*i;
    }
    printf("%.10f\n", ans);
    return 0;
}


你可能感兴趣的:(概率,codeforces,数学期望,auction,Second,price,513C)