Codeforces 801C 二分法

C. Voltage Keepsake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 000, 1 ≤ 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 .

Examples
Input
2 1
2 2
2 1000
Output
2.0000000000
Input
1 100
1 1
Output
-1

题意:n台机器,每台每单位时间耗电a,原有电量b,有一个充电器,每单位时间充电p,只能给一台机器充电,问最多过多久会有一台机器没电。若机器会一直工作下去则输出-1.
首先,可以这样考虑这个问题,设过了t时间后,所有机器不充电,那么每台机器剩下的电量为left[i]=b[i]-a[i]*t,所有left[i]>0的机器是不用充电的,对于所有left[i]<0的机器对left[i]求和,即为需要的总电量。发电机在t时间内共能提供t*p的电量,若t*p大,那么就意味着可以持续更长时间,否则减短时间。这里可以采用二分法。数据略坑,具体见注释。
代码:
//By Sean Chen
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define inf 1e10+500000       //一定要开大一点。。。。
using namespace std;

int a[100005],b[100005];
int p,n;

int check(double t)
{
    double tot=t*p;
    for (int i=0;i<n;i++)
    {
        double temp=b[i]*1.0-a[i]*1.0*t;
        if (temp<0)
            tot+=temp;
    }
    if (tot>0)
        return 1;
    return 0;
}
int main()
{
    scanf("%d%d",&n,&p);
    long long cnt=0;       //用long long
    for (int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        cnt+=a[i];
    }
    if (p>=cnt)
    {
        printf("-1\n");
        return 0;
    }
    double head=0,tail=inf,mid;
    while (tail-head>0.00000001)       //二分时间
    {
        mid=(tail+head)/2;
        if (check(mid))
            head=mid;
        else
            tail=mid;
    }
    printf("%.10lf\n",head);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值