CodeForces - 801C Voltage Keepsake(二分)

题目链接:http://codeforces.com/problemset/problem/801/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 λ·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个充电宝 每个充电宝一开始有bi电 每单位时间消耗ai电 你有一个插头 每单位时间可以给一个充电宝冲p电 问多久之后会存在一个充电宝没有电

如果一直不存在输出-1

很容易想到 当p大于等于所有充电宝耗电效率之和时 就是-1

对于此题 用模拟来做很明显超时

一开始就想到了对时间二分 但是对上界不知范围 加上不会把控判断的条件就搜题解了。。

上界定为1e18

当p*time+∑b[i]*time

但是因为这里问的是某一个充电宝到0的情况 

因此判断时用变量sum记录总共充的电 对每一个充电宝判断在过完这个时间之后sum还有没有剩余 如果出现了sum<0的情况 说明是不够用的

#include 
using namespace std;
double n,p;
double a[111111];
double b[111111];
int judge(double time)
{
	double sum=p*time;
	for(int i=0;i> n >> p;
	double sum=0;
	for(int i=0;i> a[i] >> b[i];
		sum+=a[i];
	}
	if(p>=sum)
	{
		cout << "-1" << endl;
		return 0;
	}
	double ans=0;
	double l=0;
	double r=1e18;
	while(r-l>=1e-4)
	{
		double mid=(l+r)/2;
		if(judge(mid))
		{
			ans=mid;
			l=mid;
		}
		else 
			r=mid;
	}
	printf("%lf",ans);
}  











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