【codeforces 801C】Voltage Keepsake

【题目链接】:http://codeforces.com/contest/801/problem/C

【题意】

有n个设备
你想同时使用
第i个设备每分钟消耗ai点电量,一开始有bi点电量
你有一个充电器,每分钟可以冲p点电量
问你第一次有用电器的电量耗光是在何时

【题解】

二分时间t

充电量为temp=t*p
每一个设备
    now = bi-ai*t
    如果
    now<0
        if (temp<now)
            return false;
        else
            temp-=now;
    return true;


这种小数的二分;
都按迭代次数去迭代比较好;
然后一开始的时候直接试一下最右能不能;
如果能就直接输出-1;
右端点不能太大;
可能会爆精度还是怎么样吧。


【Number Of WA

2

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;

int n,cnt = 0;
double p,a[N],b[N];
bool fi = false;

bool ok(double t)
{
    double temp = t*p;
    rep1(i,1,n)
    {
        double now = b[i]-t*a[i];
        if (now>=0) continue;
        //now<0
        if (temp+now<0) return false;
        temp+=now;
    }
    return true;
}

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin >>n>>p;
    rep1(i,1,n)
        cin >> a[i] >> b[i];
    double l = 0,r = 1e11,ans;
    if (ok(r))
    {
        return puts("-1"),0;
    }
    while (1)
    {
        cnt++;
        double m = (l+r)/2.0;
        if (ok(m))
        {
            ans = m;
            l = m;
        }
        else
            r = m;
        if (cnt==300) break;
    }
    printf("%.9f
",ans);
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626427.html