K - Keep Your Style ( 排序+数学期望值 )

K - Keep Your Style ( 排序+数学期望值 )

题目链接:http://codeforces.com/gym/101845/problem/K

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.

Examples

Input

2
2 0.5
3 1.0

Output

2.500000000

Input

2
0 0.4
20 0.6

Output

6.000000000

 

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

题目分析:

这里要求的是全部教练等待时间的总和/教练数量.教练等待时间首先和教练的排序有关系.按照贪心的思想,将花费时间短的往前排,而时间长的往后排.’
所以这里排序的cmp函数应该以 教练a的花费时间X教练a的不跑概率 < 教练b的花费时间X教练b的不跑概率
这里教练有概率不跑,也有概率跑,所以涉及期望值的一个贡献问题.可以得到这样一个递推公式.
教练i的等待时间 = (教练i-1的等待时间+教练i的花费时间)X教练i不跑的概率 + 教练i-1的等待时间X教练i逃跑的概率
最后把所有教练的等待时间加起来再求平均值即可.(我好菜啊QWQ)
 

代码:

#include <bits/stdc++.h>

using namespace std;

struct node {
    double a;
    double time,ave;
}a[500005];
double ans[500005];

bool rule( node a, node b )
{
    return a.ave<b.ave;
}

int main()
{
    int n,i,j;
    cin >> n;
    for ( i=1; i<=n; i++ ) {
        scanf("%lf %lf",&a[i].a,&a[i].time);
        a[i].ave = a[i].a*a[i].time;
    }
    sort(a+1,a+n+1,rule);
    double sum = 0;
    ans[0] = 0;
    for ( int i=1; i<=n; i++ ) {
        ans[i] = ans[i-1]*(1-a[i].time) + (ans[i-1]+a[i].a)*a[i].time;
        sum += ans[i];
    }
    printf("%.10f\n",1.0*sum/n);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值