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<cstdio>
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<n;++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<n;++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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章