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

代码2:

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const int N=1e5+;
const double eps=1e-;
int a[N],b[N];
int main()
{
int n,p;
cin>>n>>p;
ll sum=;
for(int i=;i<n;i++)
{
cin>>a[i]>>b[i];
sum+=a[i];
}
if(sum<=p)
{
cout<<-<<endl;
return ;
}
double l=,r=1e18;
while(r-l>=eps)
{
double mid=(r+l)*0.5;
double s=;
for(int i=;i<n;i++)
{
if(b[i]-a[i]*mid<)s+=a[i]*mid-b[i];
}
if(s>mid*p)r=mid;
else l=mid;
}
cout<<(r+l)*0.5<<endl;
return ;
}

Codeforces 801C - Voltage Keepsake的更多相关文章

  1. Codeforces 801C Voltage Keepsake(二分枚举+浮点(模板))

    题目链接:http://codeforces.com/contest/801/problem/C 题目大意:给你一些电器以及他们的功率,还有一个功率一定的充电器可以给这些电器中的任意一个充电,并且不计 ...

  2. Codeforces 772A Voltage Keepsake - 二分答案

    You have n devices that you want to use simultaneously. The i-th device uses ai units of power per s ...

  3. CodeForces 772A Voltage Keepsake

    二分答案,验证. 二分到一个答案,比他小的时间都需要补充到这个时间,计算所需的量,然后和能提供的量进行比较. #include <cstdio> #include <cmath> ...

  4. Voltage Keepsake CodeForces - 801C (贪心 || 二分)

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

  5. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C Voltage Keepsake

    地址:http://codeforces.com/contest/801/problem/C 题目: C. Voltage Keepsake time limit per test 2 seconds ...

  6. Codeforces801C Voltage Keepsake 2017-04-19 00:26 109人阅读 评论(0) 收藏

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

  7. 【codeforces 801C】Voltage Keepsake

    [题目链接]:http://codeforces.com/contest/801/problem/C [题意] 有n个设备 你想同时使用 第i个设备每分钟消耗ai点电量,一开始有bi点电量 你有一个充 ...

  8. Voltage Keepsake CodeForces - 801C (思维+二分)

    题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...

  9. Voltage Keepsake CodeForces - 801C(思维)

    题意: 有n台机器,第i台机器每个单位时间消耗ai的功率,初始有bi的功率储备,有一个充电器每个单位时间充p单位的功率 问经过多长时间才能有一个功率位0的机器,如果能够无限使用输出-1: 解析: 时间 ...

随机推荐

  1. Linux - TCP编程相关配置2

    100万并发连接服务器笔记之处理端口数量受限问题 第二个遇到的问题:端口数量受限 一般来说,单独对外提供请求的服务不用考虑端口数量问题,监听某一个端口即可.但是向提供代理服务器,就不得不考虑端口数量受 ...

  2. VR AR MR

    VR.AR和MR的区别? VR就是创造一个完全取代现实的世界,让人感觉「我怎么在这里?」 AR则是在现实世界的图像中叠加一些东西,让人感觉「哎?这里多了个本来不存在的东西」 MR则是虚拟物体完全和现实 ...

  3. 如何写好接口(php写app移动端接口示例)

    原文链接:https://blog.csdn.net/xwh670570759/article/details/52130585?utm_source=blogxgwz0

  4. AnswerOpenCV一周佳作欣赏(0615-0622)

    一.How to make auto-adjustments(brightness and contrast) for image Android Opencv Image Correction i' ...

  5. Babel总结

    什么是babel? babel是一个JavaScript编译器. Babel是一个工具链,主要用于将ECMAScript 2015+代码转换为向后兼容的旧浏览器或环境中JavaScript版本. 注解 ...

  6. Ant build.xml详解

    Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...

  7. 我的QML

    1.键盘加Text import QtQuick 2.7 import QtGraphicalEffects 1.0 Rectangle{ width:; height:; color:"# ...

  8. eclipse启动时发生的Initializing Java Tooling错误

    eclipse在启动发生An internal error occurred during: "Initializing Java Tooling". java.lang.Null ...

  9. if语句学习

    #print("您好,我叫周星驰") ''' x=1+2+3 print(x*4) print(x**x) a=input("请输入相应的数字a") a=int ...

  10. ODAC(V9.5.15) 学习笔记(四)TOraQuery (1)

    TOraQuery是ODAC中常用的一个组件,其继承关系如下: TDataSet ---TMemDataSet ---TCustomDADataSet ---TOraDataSet ---TCusto ...