CodeForces 772A Voltage Keepsake

题目链接:http://codeforces.com/contest/772/problem/A
题意:有n台机器,每台机器初始的电脑为b[i],每秒消耗的电能是a[i],你每秒可以提供总和为p的电能给这n台机器分,问你这n台机器最多能一起坚持几秒,如果n台机器可以无限工作下去就输出-1
解析:二分答案t,然后判一下就好

#include 
using namespace std;
const int maxn = 1e5+100;
struct node
{
    double a,b;
}a[maxn];
bool slove(double p,double t,int n)
{
    double sum  = p*t;
    for(int i=0;idouble tmp = a[i].a*t-a[i].b;
        if(tmp>0)
            sum -= tmp;
        if(sum<0)
            return false;
    }
    return true;
}
int main(void)
{
    int n;
    double p;
    scanf("%d %lf",&n,&p);
    double sum = 0;
    for(int i=0;iscanf("%lf %lf",&a[i].a,&a[i].b);
        sum += a[i].a;
    }
    double l = 0,r = 1e14;
    int k = 200;
    while(k--)
    {
        double mid = (l+r)/2;
        if(slove(p,mid,n))
            l = mid;
        else
            r = mid;
    }
    if(p>=sum)
        puts("-1");
    else
        printf("%.10f\n",l);
    return 0;
}

你可能感兴趣的:(ACM,OnlineJudge,Codeforces,ACM,二分&三分,Codeforces,漫漫补题路)