C. Voltage Keepsake

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 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 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.

题解:其实蛮简单的,只是弱太弱,所以WA了好多次。WA哭了。

给了答案需要的精度,二分即可。

当每秒的总消耗低于补充量时,输出-1;

否则,扫一遍数组,统计每个量在某个时间点消耗量,然后判断即可,移动边界。

代码如下:

//
//  main.cpp
//  C. Voltage Keepsake
//
//  Created by 徐智豪 on 2017/4/17.
//  Copyright © 2017年 徐智豪. All rights reserved.
//

#include 
#include 
#include 
#include 
using namespace std;
int n;
double p;
double sum=0;
double ai[100005]={0},bi[100005]={0};//thetime[100005]={0};
bool fun(double mid)
{
    double thesum=p*mid;
    for(int i=1;i<=n;i++)
    {
        double t=ai[i]*mid-bi[i];
        if(t>0)
            thesum-=t;
        if(thesum<=0)
            return 0;
    }
    return 1;
}
int main(int argc, const char * argv[]) {
    cin>>n>>p;
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf",&ai[i],&bi[i]);
        sum+=ai[i];
    }
    
    if(sum<=p)
    {
        cout<<-1<1e-6)
        {
            mid=(l+r)/2.0;
            if(fun(mid))
            {
                l=mid;
            }
            else
                r=mid;
        }
        cout<



你可能感兴趣的:(ACM——水题)