zoukankan      html  css  js  c++  java
  • Voltage Keepsake CodeForces

    C. Voltage Keepsake
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You have n devices that you want to use simultaneously.

    The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power stored. All devices can store an arbitrary amount of power.

    You have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for λ seconds, it will gain λ·p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.

    You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.

    If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

    Input

    The first line contains two integers, n and p (1 ≤ n ≤ 100 0001 ≤ p ≤ 109) — the number of devices and the power of the charger.

    This is followed by n lines which contain two integers each. Line i contains the integers ai and bi (1 ≤ ai, bi ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.

    Output

    If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

    Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.

    Namely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if .

    Examples
    input
    Copy
    2 1
    2 2
    2 1000
    output
    Copy
    2.0000000000
    input
    Copy
    1 100
    1 1
    output
    Copy
    -1
    input
    Copy
    3 5
    4 3
    5 2
    6 1
    output
    Copy
    0.5000000000
    Note

    In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.

    In sample test 2, you can use the device indefinitely.

    In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second.

    这题网上大多都用的二分

    先用贪心做一遍

    贪心的做法是 先用初始使用时间排序 然后 如果前i个在边充电边使用的情况下  能达到(i + 1)的使用时间 那就把前(i + 1)合并为一个仪器

    然后再判断前(i + 1)个在边充电边使用的情况下 能否使用时间达到 第(i + 2)个

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 100010, INF = 0x7fffffff;
    typedef long long LL;
    
    struct node
    {
        double t, a, b;
    }Node[maxn];
    
    double cmp(node A, node B)
    {
        return A.t < B.t;
    }
    
    int main()
    {
        int n, p;
        LL sum = 0;
        cin >> n >> p;
        for(int i = 0; i < n; i++)
        {
            cin >> Node[i].a >> Node[i].b;
            sum += Node[i].a;
            Node[i].t = Node[i].b / (double) Node[i].a;
        }
        
        if(p >= sum) return puts("-1"), 0; //只有在充电的功率大于消耗的功率的时候 才能无限使用
        
        sort(Node, Node + n, cmp);   //按初始使用时间排序
        double pow = Node[0].t * p, x_p = Node[0].a, ret = 0;
        bool flag = 0;
        int i;
        for(i = 0; i < n - 1; i++)
        {
            if(pow + (Node[i + 1].t - Node[i].t) * p > (Node[i + 1].t - Node[i].t) * x_p)
            {
                pow += (Node[i + 1].t - Node[i].t) * (p - x_p), x_p += Node[i + 1].a;
            }
            else
            {
                break;
            }
        }
        ret = pow / (double) (x_p - p);  //存的功率 除 净消耗功率 是不是就是使用时间
        ret += Node[i].t;  //然后再加上初始的使用时间
        printf("%.10f
    ", ret);
    
    
        return 0;
    }

     

     

     

    二分 :

    #include <bits/stdc++.h>
    #define eps 1e-9
    using namespace std;
    const int maxn = 100010, INF = 0x7fffffff;
    typedef long long LL;
    int n, p;
    int a[maxn], b[maxn];
    
    
    double check(double m)
    {
        double sum = 0;
        for(int i = 0; i < n; i++)
        {
            if(b[i] - a[i] * m < 0)
                sum += a[i] * m - b[i];
        }
        return p * m > sum;
    }
    
    
    
    int main()
    {
        LL sum_a = 0, sum_b = 0;
        cin >> n >> p;
        for(int i = 0; i < n; i ++)
        {
            cin >> a[i] >> b[i];
            sum_a += a[i];
        }
        if(p >= sum_a) return puts("-1"), 0;
    
        double l = 0, r = 1e18, t, m;
        while(r - l > eps)
        {
            m = (l + r) / (double)2;
            if(check(m)) l = m;
            else r = m;
        }
        printf("%.10f
    ", m);
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    ES6中map数据结构学习
    React中,input外边如果包一个div,可以把input的onChange事件绑定到div上面,并且也生效
    筛数组
    字符串slice、substring、substr
    DVA-subscriptions
    react hooks学习
    React会自动把虚拟DOM数组展开
    React+Redux+Dva学习
    [转] 关于卫星轨道的科普
    边缘计算在智慧城市中的应用【摘录】
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10631226.html
Copyright © 2011-2022 走看看