[洛谷P3743]kotori的设备

题目大意:ことり有$n$个设备,每个设备每秒共减少$a_i$能量(也就是说每一瞬间都在减少,而不是在一个时刻突然减少),开始前有$b_i$能量,ことり还有一个充电宝,无限能量,每秒共可以提供$p$的能量(也是每一瞬间都在提供),求ことり 最多可以用多久

题解:二分答案,比较这段时间中充电宝可以提供的电量和消耗的电量即可

卡点:1.$inf$设成$10^8$太小

2~6.$eps$设成$10^{-7}$太小导致$RE$

C++ Code:

#include <cstdio>
#define maxn 100010
using namespace std;
const double eps = 1e-6;
const double maxl = 1e10;
int n;
double p, a[maxn], b[maxn];
bool check(double mid) {
	double sum = p * mid;
	for (int i = 1; i <= n; i++) {
		double tmp = b[i] - a[i] * mid;
		if (tmp < 0) sum += tmp;
	}
	return sum >= 0;
}
int main() {
	scanf("%d%lf", &n, &p);
	for (int i = 1; i <= n; i++) {
		scanf("%lf%lf", &a[i], &b[i]);
	}
	double l = 0, r = maxl;
	while (r - l >= eps) {
		double mid = (l + r) / 2;
		if (check(mid)) l = mid;
		else r = mid;
	}
	if (maxl - r <= eps) puts("-1");
	else printf("%.5f", l);
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Memory-of-winter/p/9467606.html