组队赛2:Gym - 101845K 排序+数学期望值

Gym - 101845K 排序+数学期望值

题目

K. Keep Your Style
time limit per test2.5 s
memory limit per test256 MB
inputstandard input
outputstandard output
The UNAL programming coaches have lost a bet, they bet the 6 UNAL teams would occupy the first six positions in the regional finals. Now, they have to shave their heads!. Some of them have more hair than others, and consequently, it takes more time to shave a head completely. However, all of the coaches really love their hair, therefore there is a probability that some (posibly all) of them daunt at the very last moment and do not permit the hairdresser to shave their heads.

Norbert, the hairdresser who loves probability, would like to order the coaches’ schedule such that the average time in the hair salon is minimized for all the coaches. First all the coaches are there at the same time, then they start going one by one to Norbert, if by the moment a coach has to go to the hairdresser, he or she daunts then he or she simply leaves the hair salon and it is the turn of the next coach, after the head of a coach is shaved then that coach leaves the hair salon. The time between turns is negligible.

For example, suppose that shaving Diego’s head takes 2 minutes and shaving Ivan’s takes 3 minutes, but Diego has probability of 0.5 of not daunting meanwhile Ivan for sure will shave his head. If Ivan goes first, he will stay 3 minutes in the hair salon and Diego will stay there 3 minutes if daunting or 5 minutes if not (3 of them waiting for Ivan to finish), in this case the average expected time of the coaches in the hair salon would be 3.5, note this is not the optimal arrangement.

Now, Norbert knows the time it takes to shave each head and the probability of a coach to accept to have the head shaved in the barbershop, help him to know the minimum average expected time in the hair salon of the coaches.

Input
The first line of input is an integer n (1 ≤ n ≤ 5 * 105) - the number of coaches.

The next n lines contain each an integer x (0 ≤ x ≤ 100) and a decimal number y (0 ≤ y ≤ 1) separated by a single space - the time in minutes it takes to shave the head of the i - th coach and his probability of not daunting, respectively.

Output
Print the minimum expected average time. The relative error of your answer should not exceed 10 - 6.

题意

有很多人要剃头发,每个人都有剃头花费的时间和想要剃头的概率。所有人同时到,要你按某种方法排好这些人,使得他们等待时间的平均数学期望值最小,并求出这个值。

思路

排队的方法:

我们先用每个人的时间和概率,算出每个人自己剃头发的花费的时间的期望值。注意这里是自己花费的时间的期望值
然后按这个期望值从小到大排序,因为题目要求的是所有人(等待前面的人花费的时间期望值+自己花费的时间期望值)的和的平均值。所以我们要把期望值小的放到前面,这样尽可能的让后面的人少等,这样总的每个人等待的时间的期望值之和就最小。
然后拿这个最小的和(每个人的等待时间的期望值(也就是这个人前面的包括他自己的期望值之和))除以人数就得到答案了。

期望值求法:

用Ei表示第i个人的期望值
第一个人只用等他自己,时间的期望值为E1
第二个人要等第一个人和他自己,时间的期望值为E1+E2
依次下去,第三个人 E1+E2+E3
第四个人 E1+E2+E3+E4
……………………

可以看到第一个人的期望值出现了N次
第二个人的期望值出现了N-1次
………………
最后一个人的期望值出现了1次

所以要求总的等待时间的期望值之和,就直接遍历一下,乘以出现次数并求和,然后除以人数就好了。

反思

这道题属于水题,不过给卡住了。
一开始以为每个人都有要剃头或者不剃头的状态,分下去是个完全二叉树,直接2^n -1,n可以到3e5,会TLE。
后来想到把它们的时间和概率相乘,然后从小到大排序,是最佳的策略,然后接下来的数学期望又不知道怎么算了,真的是傻屌了当时。

代码

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int n;
struct node
{
	int x;
	double y;
	double e;
}a[500010];

int cmp(struct node a,struct node b)
{
	return a.e<b.e;
}
int  main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d %lf",&a[i].x,&a[i].y);
		a[i].e=1.0*a[i].x*a[i].y;
	}
	sort(a+1,a+1+n,cmp);
	double sum=0;
	
	for(int i=1;i<=n;i++)//求和 
		sum+=a[i].e*(n-i+1);	
		
	printf("%.12lf\n",sum*1.0/n);
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值