CF#772 A. Voltage Keepsake(二分)

来源:互联网 发布:淘宝保证金管理在哪里 编辑:程序博客网 时间:2024/06/03 18:27
A. 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 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 .

Examples
input
2 12 22 1000
output
2.0000000000
input
1 1001 1
output
-1
input
3 54 35 26 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台机器,每台机器单位时间内耗电 a[ i ] ,开始时有 b[ i ] 的电,同时还有一个充电器可以在单位时间内给任意一台机器充 p 的电,问所有机器一同工作的最长时间。


#include<bits/stdc++.h>#define eps 1e-10using namespace std;const int N = 100000 + 10 ;int n;double a[N],b[N],p;bool judge(double mid){    double sum=0;    for(int i=0;i<n;i++)    {        if(a[i]*mid-b[i]>eps)            sum+=a[i]*mid-b[i];    }    if(p*mid-sum<eps) return 1;    return 0;}int main(){    while(scanf("%d%lf",&n,&p)==2)    {        for(int i=0;i<n;i++)        {            scanf("%lf%lf",&a[i],&b[i]);        }        double l=0,r=1e10,ans=0;        for(int i=0;i<100;i++)        {            double mid=(l+r)/2.0;            if(judge(mid)) ans=mid,r=mid;            else l=mid;        }        if(ans) printf("%.10f\n",ans);        else printf("-1\n");    }}


0 0
原创粉丝点击