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.
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.
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 .
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.
题意:有n个设备,每个设备每单位时间内耗电a[i],其初始电量为b[i]。现有一个单线充电器,每单位时间内可以充电p单位,且可以在任意时间(包括实数)内拔充。
当设备电量为0时,停止工作,如果所有设备都可一直工作下去,输出-1,临界条件0也属于可以一直工作下去的范畴,否则,输出所有设备都同时工作的最大时间。
思路:二分时间,假设所有设备同时工作的最大时间为t(也可认为是充电总时间),则t时间内充电器总供能p * t单位,这时t * p == sum(a[i] * t) - sum(b[i]);当t * p > sum(a[i] * t) - sum(b[i])时,充电器供大于求,故更新下界(增大充电时间),当t * p < sum(a[i] * t) - sum(b[i])时,充电器供不应求,应更新上界(缩减充电时间)
还有计算a[i]总和的sum应定义为long long,1e10 ≈ 2^33,2^33 = 85 8993 4592,2^34 = 171 7986 9184
当计算结果和标准答案误差在指定范围内就认为通过
思路是拷贝来的
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 7;
const double EPS = 1e-4;
int n, p;
int a[MAXN], b[MAXN];
double check(double t){
double tot = p * t;
for(int i = 0; i < n; i++){
double tmp = b[i] - a[i] * t;
if(tmp < 0)//说明入不敷出
tot += tmp;
}
return tot;
}
int main(){
cin >> n >> p;
long long sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
sum += a[i];
}
if(p >= sum){//等于是为临界条件,也应输出-1
cout << "-1" << endl;
return 0;
}
double l = 0, r = 1e10;
double mid;
while(r - l >= EPS){//实数域上二分时差值不可能为0,只能以精度控制
mid = (l + r) / 2;
//当check(mid) == 0,说明刚好满足
if(check(mid) > 0)//还有剩余的电,说明时间小了,增加用电时间
l = mid;
else //电量不够,缩减用电时间
r = mid;
}
printf("%.10f\n", mid);
return 0;
}