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 000, 1 ≤ 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.
OutputIf 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 .
2 1 2 2 2 1000
2.0000000000
1 100 1 1
-1
3 5 4 3 5 2 6 1
0.5000000000
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和Tde狠心塞精度问题 那个eps改成1e5或者 1e4就可以过了 但是如果是其他的有的t有的wa
这个东西我也不知道怎么说 还有一个就是自己对于二分的掌握度还不够 就是那个边界条件都无法判断心狠塞啊 啊啊啊啊啊
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 100050;
const double eps = 1e-4;
double n,p,sum=0;
struct node
{
double a,b;//a表示消耗多少能量 b表示说每秒充多少能量
}s[maxn];
bool judge(double t)
{
double sum = p*1.0*t;
double sub;
for(int i = 1;i<=n;i++)
{
sub = s[i].a * t - s[i].b;
if(sub > 0) sum-=sub;
if(sum<0)return false;
}
return true;
}
db Two_find()
{
db left = 0,right = 1e10,mid,ans;
while(left+eps<=right)
{
double mid = (left+ right)/2;
if(judge(mid))
left = mid,ans = mid;
else right = mid;
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>p;
for(int i = 1;i<=n;i++)
{
cin>>s[i].a>>s[i].b;
sum+=s[i].a;
}
if(p>=sum){
printf("-1\n");
return 0;
}
db ans = Two_find();
printf("%.8lf",ans);
return 0;
}