水题

本文介绍了一道关于多台设备在特定充电条件下的最大工作时长算法题。通过二分查找法来确定设备组能在不耗尽电量的情况下运行的最大时间。分析了题目要求,给出了详细的解题思路及C语言实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You have n devices that you want to use simultaneously.

The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power stored. All devices can store an arbitrary amount of power.

You have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for λ seconds, it will gain λ·p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.

You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

Input

The first line contains two integers, n and p (1 ≤ n ≤ 100 0001 ≤ p ≤ 109) — the number of devices and the power of the charger.

This is followed by n lines which contain two integers each. Line i contains the integers ai and bi (1 ≤ ai, bi ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.

Output

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.

Namely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if .

Example
Input
2 1
2 2
2 1000
Output
2.0000000000
Input
1 100
1 1
Output
-1
Input
3 5
4 3
5 2
6 1
Output
0.5000000000
Note

In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.

In sample test 2, you can use the device indefinitely.

In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second.

题意:给定 n 台机器同时使用,第 i 台机器每秒消耗能量 ai ,初始能量为 bi 。有一个充电器能给 n 台机器充电,同一时间只能给一台机器充电,每秒充能量 p 。其中充电不必按整秒进行,即例如给第 1 台充 0.001 秒,给第 2 台充 0.12 秒是合法的,且不必考虑交换充电机器的时间。问 n 台机器最多能同时工作多少时间?

结题思路:用二分进行枚举能够同时工作 t 秒,再对 t 秒判断每台机器是否都能够坚持 t 秒钟。

判断 n 台机器是否能坚持至少 t 秒:在 t 秒时间充电器能够充能量总值为 pt 。枚举判断 n 台机器能否坚持 t 秒,对于能量不足的机器,从 pt 的总值中扣除不足部分。若最终 pt 仍有剩余,说明至少能坚持 t 秒,否则不能。


#include<stdio.h>
#define eps 1e-7
#define N 100010
int m;
double a[N],b[N],n;
int juge(double t)
{
    double x=n*1.0*t,y;
    int j;
    for(j=1;j<=m;j++)
    {
        y=a[j]*t-b[j];
        if(y>0) x=x-y;
        if(x<0) return 0;
    }
    return 1;
}
int main()
{
    double sum=0;
    int i;
    scanf("%d %lf",&m,&n);
    for(i=1;i<=m;i++)
    {
        scanf("%lf %lf",&a[i],&b[i]);
        sum=sum+a[i];
    }
    if(n-sum>=-eps)
    {
        printf("-1\n");
        return 0;
    }
    double l=0,r=1e12,mid,ans;
    while(r-l>eps)
    {
        mid=(l+r)/2;
        if(juge(mid))
        {
            l=mid;
            ans=mid;
        }
        else r=mid;
    }
    printf("%.8lf\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值