Keep Your Style (贪心排序+概率) [gym-101845K]

原题传送门


题面

Keep Your Style

                               time limit per test : 2.5 s
                          memory limit per test : 256 MB
                                 input : standard input
                               output : standard output

problem describe

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 * 1e5) - 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 1e-6.

Sample Input(1)

2
2 0.5
3 1.0

Sample Output(1)

2.500000000

Sample Input(2)

2
0 0.4
20 0.6

Sample Output(2)

6.000000000

题目描述

第i个教练需要花费ai的时间来剪头发,并且有一定的概率不会离开.求出全部教练等待时间的和的平均值的最小值.

题目分析

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

具体代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5*1e5+7;

struct node
{
    int t;
    double p;
};

bool cmp(node a, node b)
{
    return a.p*a.t < b.p*b.t;
}

node c[maxn];
double ans[maxn];
int n;

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d%lf", &c[i].t, &c[i].p);
    sort(c+1, c+1+n, cmp);
    for(int i = 1; i <= n; i++)
    {
        ans[i] = ((ans[i-1]+c[i].t)*c[i].p)+(ans[i-1]*(1.0-c[i].p));
    }
    double sum = 0;
    for(int i = 1; i <= n; i++) sum += ans[i];
    printf("%lf", sum/n*1.0);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值