double二分(P3743 小鸟的设备)
题目:P3743 小鸟的设备 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
double a[N],b[N];
int n;
double p;bool check(double mid)
{double sum=0.0;for(int i=1;i<=n;i++){if(a[i]*mid>b[i])sum+=a[i]*mid-b[i];}return p*mid>=sum;
}
int main()
{cin>>n>>p;double sum=0.0;for(int i=1;i<=n;i++){cin>>a[i]>>b[i];sum+=a[i];}if(p>=sum){cout<<-1;return 0;}double l=0,r=1e10;while(r-l>1e-5){double mid=(l+r)/2.0;if(check(mid))l=mid;elser=mid;}cout<<l;return 0;
}