codforces Codeforces Round #409 div2 C 二分搜索

题目:
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 λ·aiunits 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 0power.

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

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 / 10of a second.

第一道被fst的题,在cf上第一道WA超过20次的题,本来开始还不服气的,这个题fst了,后来相当服气:

1,二分搜索一定要事先计算答案的上下限,不要靠手感,随手一打就是8个9。

2,涉及到需要高精度时少用除法,多用乘法。

思路:见代码吧。

code:

#include
const int MAXN=1e5+5;
const long long INF=1e12+5;
int n,p;
int a[MAXN],b[MAXN];
bool C(double x){
    double tot=0.0;
    for(int i=0;i         if(b[i]-a[i]*x<=0)tot+=a[i]*x-b[i];//tot表示n个device如果要
    }//用到那么长时间至少还需要多少电量
    return tot<=x*p;//x*p表示充电器在x的时间内能提供的电量。
}//如果不好理解可以先用除法形式再改成乘法tot/p<=x
int main(){
    scanf("%d%d",&n,&p);
    long long suma=0;
    for(int i=0;i         scanf("%d%d",a+i,b+i);
        suma+=a[i];
    }
    if(suma<=p){printf("-1\n");return 0;}//无穷大条件,sigma a[i]<=p;
    double lo=0.0,hi=1.0*INF;
    for(int i=0;i<300;++i){//二分答案
        double mid=(hi+lo)/2.0;
        if(C(mid))lo=mid;
        else hi=mid;
    }
    printf("%.8lf\n",lo);
}

你可能感兴趣的:(bfs&dfs)