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. udp丢包 处理

    转自: 自己在做UDP传输时遇到的问题,接收端没设置缓存,结果总是丢包. 看到这篇文章设置了一下接收缓存就好 *;//设置为32K setsockopt(s,SOL_SOCKET,SO_RCVBUF, ...

  2. 如何搭建一个 MySQL 分布式集群

    1.准备集群搭建环境 使用6台虚拟机来搭建 MySQL分布式集群 ,相应的实验环境与对应的MySQL节点之间的对应关系如下图所示: 管理节点(MGM):这类节点的作用是管理MySQLCluster内的 ...

  3. libopencv_shape.so.3.0: cannot open shared object file: No such file or directory 解决笔记

    进入目录:/etc/ld.so.conf.d 创建:opencv.conf 添加:/opt/opencv-3.0.0/build/lib 执行:ldconfig DETAIL: (1)ldd dlsd ...

  4. 寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)

    一.问题提出 这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-hol ...

  5. Python 基础指令

    ## Python 基础指令 ```Shell $ pip install ipython== # 安装指定版本的python第三方库 $ python --version #查看版本 $ which ...

  6. QML的Window与ApplicationWindow

    ApplicationWindow需要导入QtQuick.Controls Window需要导入QtQuick.Window . 默认不可见,需要设置visible:true才可见. 主要区别就是Ap ...

  7. topcoder srm 690 div1 -3

    1.给定一个数字$N$,从1到100的100个数字中选出$K$个数字(设为集合$S$),然后对$S$进行如下运算: (1)删除$S$中的某些数字:(可以删除0个数字) (2)将$S$中的某些数字取为它 ...

  8. topcoder srm 709 div1

    1 给定一个长度为n的整数数组A,重排列数组A使得下面计算出的X最大:(n不大于15,A中的大于等于0小于等于50) int X=0; for(int i=0;i<n;++i) X=X+(X^A ...

  9. 再谈 linux 的sed用法

    很多东西, 如果太复杂, 太庞杂, 一开始, 可以只掌握最简单的. 如果连最简单的都不能掌握, 那看那么多有什么用? 关于cut, sed的 处理过程和思想? vm: virtual machine, ...

  10. Git 配置命令设置

    目录 查看配置文件路径: 查看其他配置命令: 修改配置文件默认路径: 查看配置文件路径: 查看系统级别配置: git config -e –-system D:/Program Files/Git/m ...