【打CF,学算法——三星级】CodeForces 513C Second price auction (进制状态表示)

【CF简介】

提交链接:CF 513C


题面:

C. 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. Thei-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range betweenLi andRi, 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 andRi (1 ≤ Li ≤ Ri ≤ 10000) describing thei-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.

Examples
Input
3
4 7
8 10
5 5
Output
5.7500000000
Input
3
2 5
3 4
1 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 probability0.5 the first company will bid at most 5 microdollars, 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 bid7 microdollars. Thus, the expected value the second company will have to pay is0.5·5 + 0.25·6 + 0.25·7 = 5.75.

题意:

    一堆公司在自己的固定区间内竞拍广告价格,价高者得,但最高价者只需支付第二高的价格,问需要支付的价格的期望。


解题:

    枚举取得每个价格,首先用三进制表示合法的状态,0表示和取得的价格相同,1表示大于取得的价格,2表示小于取得的价格。合法的状态只有两种,

    1.一个1,x(x>0)个0,任意个2。

    2.零个1,x(x>=2)个0,任意个2。

    随后计算每个取得的值的概率,为每个公司出价的区间除以其出价总区间。

    期望为每个值乘以对应的概率的累加。

注意:

    注意对可取边界的讨论,可能整个区间都在取得值的一侧,或无法取得。


代码:

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int le[6],ri[6];
int n,cnt;
string status[250];
bool check(string s)
{
	int cnt0=0,cnt1=0,cnt2=0;
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='0')
			cnt0++;
		else if(s[i]=='1')
			cnt1++;
		else
			cnt2++;
	}
	if(cnt1==1&&cnt0)
		return 1;
	if(cnt1==0&&cnt0>=2)
		return 1;
	return 0;
}
void dfs(string s)
{
  if(s.length()==n)
  {
     if(check(s))
		 status[cnt++]=s;
	 return;
  }
  dfs(s+'0');
  dfs(s+'1');
  dfs(s+'2');
}
double cal(int x,int y)
{
	double res=1.0*x;
	for(int i=0;i<n;i++)
	{
      if(status[y][i]=='0')
	  {
		  if(x>=le[i]&&x<=ri[i])
		  {
			  res*=1.0/(ri[i]-le[i]+1);
		  }
		  else
			  return 0.0;
	  }
	  else if(status[y][i]=='1')
	  {
		  if(ri[i]>x)
		  {
			  if(le[i]<=x)
			    res*=1.0*(ri[i]-x)/(ri[i]-le[i]+1);
			  else
				res*=1.0;
		  }
		  else
			  return 0.0;
	  }
	  else
	  {
		  if(le[i]<x)
		  {
			  if(ri[i]>=x)
			    res*=1.0*(x-le[i])/(ri[i]-le[i]+1);
			  else
				res*=1.0;
		  }
		  else
			  return 0.0;
	  }
	}
	return res;
}
int main()
{
	int minn,maxn;
	double ans=0.0;
	cnt=0;
	scanf("%d",&n);
	dfs("");
	minn=10010,maxn=0;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&le[i],&ri[i]);
		minn=min(minn,le[i]);
		maxn=max(maxn,ri[i]);
	}
    for(int i=minn;i<=maxn;i++)
	{
		for(int j=0;j<cnt;j++)
			ans+=cal(i,j);
	}
	printf("%.9lf\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值