ZOJ 3334 Body Check (二分)

12 篇文章 0 订阅

Due to the spread of H1N1 all over the world, the king of Kingdom atouSK is so afraid that he decides to ask all his people to do a body check, no matter how much money it will take. Thus, the king calls up m of the best doctors across the kingdom to finish the task. The docotrs of Kingdom atouSK are selfish. If some of them are asked to work while others of them can take a rest, the docotrs who need to work will get angry and make mistakes at their work time. However, there is one exception, when just one of them needs to work and the others can have a rest, the other doctors will be united and report to the king if the working doctor makes mistakes, so the working doctor needs to do the job alone carefully (How poor!). Therefore, all the m docotrs need to work together or only one of them does the work at certain time.

There are n people in Kingdom atouSK, and it takes different people differnt time to finish the body check. A person can do parts of the check by one doctor and later move to another doctor to continue the check. In other words, a person can do any part of the body check by any docotr at any time, all depending on the doctors’ arrangement. But of course, a person CANNOT do the check by two or more doctors at the same time! Suppose the chosen m docotrs are so excellent that they all work at the same efficiency, but they should be focused and each CANNOT check more than one people at the same time.

Though the king doesn’t care about the money, he is so concerned about the time to finish all the checks, because the later all the checks finish, the more likely the disease will spread. The king is too busy, so he asks you to calculate the shortest time needed to finish all the body checks.

m 个医生给 n 个病人进行身体检查,已知 n 个人每人所需的检查时间为 ti (可中断,或由多个医生共同完成检查,但同一时间不能被多个医生一起检查)。m 个医生的检查工作必须满足下列规则中的一条:

  1. 所有医生在同一时间段 [l,r] 内都在检查不同的病人。
  2. 只有一个医生在检查一个病人的身体,其他医生均在休息。
  3. 所有医生都在休息。

问检查所有 n 个病人身体所需的最少时间?

解题思路

对于 n 个病人,要使得最少时间内完成检查,首先在能满足条件 1 的情况下,所有医生应尽可能都去检查。当无法满足条件 1 时,才考虑单个医生进行检查。

若假设共同检查的时间为 x ,则完成全部检查所需的时间为 Σtim×x+x

由于每个病人的检查时间可无限分割,且可由不同的医生完成检查。

故对于共同检查的时间 x 可由二分得到:

n 个病人在共同检查期间的总用时为 Σni=1min(ti, x) 。其中共同检查总共可消耗 m×x 的时间。

即若 Σni=1min(ti, x)<m×x ,则 x 仍可继续缩小;否则,x 必须更大方可。

代码

#include<bits/stdc++.h>
using namespace std;
int n, m;
double t[1010], tot, dig;
int cmp(double x)
{
    if(abs(x) < 1e-8)   return 0;
    return x > 0 ? 1 : -1;
}
int main()
{
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        tot = 0.0;
        for(int i=1;i<=n;i++)
            scanf("%lf",&t[i]), tot += t[i];
        double l = 0.0, r = tot, mid, ans = 0.0;
        while(cmp(r-l))
        {
            mid = (l+r) / 2;
            dig = 0.0;
            for(int i=1;i<=n;i++)
                dig += min(mid, t[i]);
            if(cmp(dig - mid*m) < 0)
                ans = mid,  r = mid;
            else
                l = mid;
        }
        printf("%.10lf\n", tot-m*ans+ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值