如题,就是很疑惑。不应该有死循环吧?
#include<bits/stdc++.h>
using namespace std;
int n, p, cost[100001], deposit[100001];
long long costSum = 0, depositSum = 0;
double test(double mid){
double energy = 0, tmp;
for(int i = 0; i<n; ++i) {
tmp = deposit[i] - mid * cost[i];
if(tmp < 0) energy -= tmp;
}
return energy;
}
int main(){
scanf("%d%d", &n, &p);
double l = 0, r = 0, mid, tmp;
for(int i = 0; i<n; ++i){
scanf("%d", &cost[i]);
scanf("%d", &deposit[i]);
costSum += cost[i];
depositSum += deposit[i];
}
r = 1e10;
if(p >= costSum){
printf("-1");
return 0;
}
while(l < r){
mid = l + (r - l)/2;
if(test(mid) >= p * mid){
r = mid;
}else{
l = mid + 10e-5;
}
}
printf("%f", l);
return 0;
}