ACM-ICPC World Finals 2017 - Need for Speed

时间:2021-02-23 18:21:46

Need for Speed
题意:t=(di/(si+c)+…+(dn/(sn+c)));求c。
思路:二分,精度卡的有点厉害,主要是学学做法。

#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
const int maxn=1005;
const int inf=0x3f3f3f3f;
int n;
double t;
int a[maxn],b[maxn];
int solve(double c)
{
    double ans=0.0;
    for(int i=0;i<n;i++)
    {
        if((c+b[i])<=0) return -1;
        else ans+=1.0*(a[i]/(c+b[i]));
    }
    if(ans<=t) return 1;
    else return -1;
}
int main()
{
    while(~scanf("%d%lf",&n,&t))
    {
        mem(a,0);
        mem(b,0);
        for(int i=0;i<n;i++)
            scanf("%d%d",&a[i],&b[i]);
        double l=-1e9,r=1e9;
        while((r-l)>1e-9)
        {
            double mid=(l+r)/2;
            if(solve(mid)>0) r=mid;
            else l=mid;
        }
        printf("%.6lf\n",l);
    }
    return 0;
}