Second price auction - CodeForces 513 C 概率期望

来源:互联网 发布:yes风淘宝客程序下载 编辑:程序博客网 时间:2024/06/09 23:03

Second price auction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random.

However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot.

Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between Li and Ri, inclusive. In the other words, the value of thei-th company bid can be any integer from the range [Li, Ri] with the same probability.

Determine the expected value that the winner will have to pay in a second-price auction.

Input

The first line of input contains an integer number n (2 ≤ n ≤ 5). n lines follow, the i-th of them containing two numbers Li and Ri (1 ≤ Li ≤ Ri ≤ 10000) describing the i-th company's bid preferences.

This problem doesn't have subproblems. You will get 8 points for the correct submission.

Output

Output the answer with absolute or relative error no more than 1e - 9.

Sample test(s)
input
34 78 105 5
output
5.7500000000
input
32 53 41 6
output
3.5000000000
Note

Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5·5 + 0.25·6 + 0.25·7 = 5.75.


题意:求投标的第二大的期望。

思路:枚举第二大的数为x。这时,第一种情况是有一个数>x,其余<=x且至少有一个为x,第二种情况为所有<=x且至少有两个为x。P(<=x且至少有一个为x)=p(<=x)-p(<x),P(<=x且至少有两个为x)=P(<=x且至少有一个为x)-P(<=x且恰有一个为x)。

AC代码如下:

#include<cstdio>#include<cstring>using namespace std;int n,l[10],r[10];double ans=0;double P1(int a,int k)//>{    if(k<l[a])      return 1;    if(k>=r[a])      return 0;    return 1.0*(r[a]-k)/(r[a]-l[a]+1);}double P2(int a,int k)//={    if(k<l[a] || k>r[a])      return 0;    return 1.0/(r[a]-l[a]+1);}double P3(int a,int k) //<{    if(k>r[a])      return 1;    if(k<=l[a])      return 0;    return 1.0*(k-l[a])/(r[a]-l[a]+1);}double P4(int a,int k)  //<={    if(k>=r[a])      return 1;    if(k<l[a])      return 0;    return 1.0*(k-l[a]+1)/(r[a]-l[a]+1);}void solve1(int a,int k){    int i;    double p=1,p2=1,p3=1;    p*=P1(a,k);    for(i=1;i<=n;i++)       if(i!=a)       {           p2*=P4(i,k);           p3*=P3(i,k);       }    p*=p2-p3;    ans+=p*k;}void solve2(int k){    int i,j;    double p=1,p2=1,p3=1,p4=1;    for(i=1;i<=n;i++)    {        p2*=P4(i,k);        p3*=P3(i,k);    }    p*=p2-p3;    for(i=1;i<=n;i++)    {        p4=P2(i,k);        for(j=1;j<=n;j++)           if(i!=j)             p4*=P3(j,k);        p-=p4;    }    ans+=p*k;}int main(){    int i,j,k;    scanf("%d",&n);    for(i=1;i<=n;i++)       scanf("%d%d",&l[i],&r[i]);    for(k=10000;k>=1;k--)    {        for(i=1;i<=n;i++)           solve1(i,k);        solve2(k);    }    printf("%.10f\n",ans);}



0 0
原创粉丝点击