You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const double eps = 1e-6;
const double MAX = 1e10;
//为什么上限要是1e10?浏览了很多题解都说是浮点数二分题建议设为1e10
//卡点:
//1.MAX设成10^10太小
//2.eps设成108太小导致第19个测试点TLE提示我们减小精度
int n;
double p; //每秒可以给接通的设备充能p个单位
double a[N]; //第i个设备每秒消耗ai个单位能量。
double b[N]; //在开始的时候第i个设备里存储着bi个单位能量。
//检查函数
//含义在x这个时长是否能实现所有设备电能不为零
bool check(double x) {
//x: 最多能使用多久
double sum = p * x; //一共能充多少电
//遍历每一个设备
for (int i = 1; i <= n; i++) {
double t = b[i] - a[i] * x; //设备的原电量减去在x这段时间内的消耗电量。
// 如果大于等于0表示不需要充电如果小于0表示需要充电
if (t < 0) sum += t; //需要充电t这么多的电量这些电量需要在sum中扣除掉。
}
return sum >= 0; //如果最终sum还是大于等于0的表示这个x的时间是可以实现的。还可以再大一点试试
}
int main() {
//读入数据
cin >> n >> p;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
//特判(所有设备的每秒耗电量和<=充电宝的每秒充电量,那么是永久性的.输出-1
double s = 0;
for (int i = 1; i <= n; i++) s += a[i];
if (p >= s) {
puts("-1");
exit(0);
}
//浮点数二分
double l = 0, r = MAX;
while (r - l >= eps) {
double mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
printf("%.5f", l);
return 0;
}