zoukankan      html  css  js  c++  java
  • Codeforces 801C

    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 000, 1 ≤ 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
    2 1
    2 2
    2 1000
    output
    2.0000000000
    input
    1 100
    1 1
    output
    -1
    input
    3 5
    4 3
    5 2
    6 1
    output
    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.

    题目大意:有n个装备,每个设备耗能为每单位时间耗能ai,初始能量为bi;你有一个充电宝,每单位时间可以冲p能量,你可以在任意时间任意拔冲。

    如果可以所有设备都可以一直工作下去,输出-1;否则,输出所有设备都同时工作的最长时间。

    思路提示:想象这样一个场景,每当一个设备没电时,你就拔掉你正在充电的设备,冲到这个设备上。可是,天有不测风云,突然某一刻,有2个以上设备同时没电,那至少有一个设备得停止工作。这一刻也就是答案,让我们来看一看这一刻的状况,设这一刻为t,充电宝总共供能t*p,这时t*p==sum(a[i]*t)-sum(b[i]);当t*p<sum(a[i]*t)-sum(b[i])说明t比答案大;当t*p>sum(a[i]*t)-sum(b[i])说明,t比答案小。

    方法:在实数域上二分,不断逼近答案。

    代码1:

    #include<iostream>
    #include<cstdio>
    #define ll long long
    using namespace std;
    const int N=1e5+5;
    const double eps=1e-4;//精度
    int n,p;
    int a[N],b[N];
    int check(double mid)
    {
        double E=mid*p; 
        for(int i=0;i<n;i++)
        {
            double e=b[i]-a[i]*mid;
            if(e<0)E+=e;
            if(E<0)return 0;//如果充电宝的能量不足以供应,说明mid太大
        }
        return 1;//否则mid太小
    }
    int main()
    {
        cin>>n>>p;
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
            sum+=a[i];
        }
        if(sum<=p)
        {
            cout<<-1<<endl;
            return 0;
        }
        double low=0,high=1e18;
        double mid;
        while(high-low>=eps)//实数域上的二分不可能为0,只能以精度控制
        {
            mid=(high+low)/2;
            if(check(mid))low=mid;
            else high=mid;
        } 
        cout<<mid<<endl;
        return 0;
    }

     代码2:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    #define ll long long
    const int N=1e5+5;
    const double eps=1e-4;
    int a[N],b[N];
    int main()
    {
        int n,p;
        cin>>n>>p;
        ll sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i]; 
            sum+=a[i];
        } 
        if(sum<=p)
        {
            cout<<-1<<endl;
            return 0;
        }
        double l=0,r=1e18;
        while(r-l>=eps)
        {
            double mid=(r+l)*0.5;
            double s=0;
            for(int i=0;i<n;i++)
            {
                if(b[i]-a[i]*mid<0)s+=a[i]*mid-b[i];
            }
            if(s>mid*p)r=mid;
            else l=mid;
        }
        cout<<(r+l)*0.5<<endl;
        return 0;
    }
  • 相关阅读:
    webpack中文网的错误&&未更新内容(webpack4)
    本地运行别人的vue项目;新建一个vue项目;打包并部署一个vue项目
    ajax/JSON
    弹性盒模型,FLEX
    opencv-python-学习笔记五(图像的基本操作)
    opencv-python-学习笔记四(创建滑动条)
    opencv-python-学习笔记三(鼠标事件)
    opencv-python-学习笔记二(利用opencv绘图)
    opencv-python-学习笔记一(图像的读取与写入)
    开发Koa 必须用的插件
  • 原文地址:https://www.cnblogs.com/widsom/p/6725718.html
Copyright © 2011-2022 走看看