Codeforces Round #409 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.


传送门

给定 n 台机器同时使用,第 i 台机器每秒消耗能量 ai ,初始能量为 bi 。有一个充电器能给 n 台机器充电,同一时间只能给一台机器充电,每秒充能量 p 。其中充电不必按整秒进行,即例如给第 1 台充 0.001 秒,给第 2 台充 0.12 秒是合法的,且不必考虑交换充电机器的时间。问 n 台机器最多能同时工作多少时间?

若 n 台机器能够无限工作,输出 -1 。

解题思路

当且仅当 Σai≤p 时,能够无限工作。

二分进行枚举能够同时工作 t 秒,再对 t 秒判断每台机器是否都能够坚持 t 秒钟。

判断 n 台机器是否能坚持至少 t 秒:在 t 秒时间充电器能够充能量总值为 p∗t 。枚举判断 n 台机器能否坚持 t 秒,对于能量不足的机器,从 p∗t 的总值中扣除不足部分。若最终 p∗t 仍有剩余,说明至少能坚持 t 秒,否则不能。

#include 
//#include 
//#include 
//using namespace __gnu_pbds;
using namespace std;


#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0);
#define srand() srand(time(0));
typedef long long LL;
typedef pair pii;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
//const int dx[]={-1,0,1,0,-1,-1,1,1};
//const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const int MAX_N=1000006;
const int MAX_Tot=500005;
const double EPS=1e-5;
const int MOD=1000000007;
#define mod(x) ((x)%MOD);
templateinline T min(T a,T b,T c) { return min(min(a,b),c);}
templateinline T max(T a,T b,T c) { return max(max(a,b),c);}
templateinline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
templateinline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree,rb_tree_tag,tree_order_statistics_node_update> rbtree;
/*lch[root] = build(L1,p-1,L2+1,L2+cnt);
    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/
/*lch[root] = build(L1,p-1,L2,L2+cnt-1);
    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}

inline int Scan()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')flag=1;
    else if(ch>='0' && ch<='9')res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
    return flag ? -res : res;
}

int n,p;
struct node
{
    int a,b;
}Q[maxx];

bool check(double x)
{
    double sum = 0;
	for (int i = 1; i <= n; i++) sum += max(0.0, Q[i].a * x - Q[i].b);
    if (sum < x*p) return true;
	else return false;
}

int main()
{
    cin>>n>>p;
    double t=0;
    for(int i=1;i<=n;i++)
    {
        cin>>Q[i].a>>Q[i].b;
        t+=Q[i].a;
    }
    if(t<=p)
    {
        cout<<-1<EPS)
    {
        double mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else r=mid;
    }
    printf("%.8lf\n",l);
}


你可能感兴趣的:(二分法)