VMC 时间管理大师
时间管理大师
比较裸的二分。
二分答案,因为可以无缝切换,直接比较所需的充电电量和实际可以充的电量即可。
代码如下:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline int read();
const int N = 1e5 + 5;
const double eps = 1e-6;
int n, e;
struct P {
int a, b;
} cp[N];
bool check(double t) {
double ned = 0;
for (int i = 1; i <= n; ++i) {
ned += max(0.0, t * cp[i].a - cp[i].b);
}
return ned < t * e - eps;
}
int main() {
n = read(), e = read();
ll sumA = 0;
for (int i = 1; i <= n; ++i) {
cp[i].a = read(), cp[i].b = read();
}
double l = 0, r = 1e9, ans;
while (l < r - eps) {
double mid = (l + r) / 2;
check(mid) ? l = mid, ans = mid : r = mid;
}
if (ans >= 1e9 - eps)
printf("-1");
else
printf("%lf", ans);
return 0;
}
inline int read() {
int x = 0;
char c = getchar();
bool f = 1;
while (!isdigit(c))
f = c ^ 45, c = getchar();
while (isdigit(c))
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return f ? x : -x;
}
这里说明一下,实数二分可能因为精度问题一直卡住,最好还是使用固定次数二分的方法。
本文来自博客园,作者:Maplisky,转载请注明原文链接:https://www.cnblogs.com/lbh2021/p/18722593