Codeforces 801C Voltage Keepsake【二分】

探讨了给定多个设备及其初始电量和耗电速率的情况下,利用单一充电器实现最优电量分配的方法,以最大化所有设备可用时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个需要充电的物品,每个物品一开始有bi的电量,每一单位时间消耗ai的电量。现在有一个充电器,每一单位时间可以补充P点电量,问你第一次出现有物品没电了的时刻。


思路:


时间具有单调性,那么考虑二分时间,接下来对于每个物品的时间总用电量进行统计,然后对应将每个物品需要的充电量进行统计,对于这个统计的量和p比较大小即可。

如果这个统计量小于等于p,那么增大时间,相反,减小时间。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
#define eps 1e-6
struct node
{
    double x,y;
}a[108000];
int n;
double p;
int Slove(double mid)
{
    double sum=0;
    for(int i=0;i<n;i++)
    {
        double yu=a[i].y-a[i].x*mid;
        if(yu<0)sum+=fabs(yu);
    }
    if(sum+eps<=p*mid)
    {
        return 1;
    }
    else return 0;
}
int main()
{
    while(~scanf("%d%lf",&n,&p))
    {
        for(int i=0;i<n;i++)scanf("%lf%lf",&a[i].x,&a[i].y);

        double ans=-1;
        double l=0;
        double r=120;
        if(Slove(r)==0)
        {
            printf("-1\n");
            continue;
        }
        for(int i=0;i<80;i++)
        {
            double mid=(l+r)/2;
            if(Slove(mid)==1)
            {
                ans=mid;
                l=mid;
            }
            else r=mid;
        }
        if(ans>1200000000000)printf("-1\n");
        else
        printf("%lf\n",ans);
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值