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. 分布式系统下的全局id生成策略分析

    对于分布式系统而言,意味着会有很多个instance会并发的生成很多业务数据,比如订单.不同的机房.不同的机器.不同的应用实例会同时生成.所以,如何生成一个好用的全局id并不是一个简单的uuid就能够 ...

  2. 2、pandas的value_counts()和describe()

    一.value_counts pandas 的value_counts()函数可以对Series里面的每个值进行计数并且排序. value_counts是计数,统计所有非零元素的个数,默认以降序的方式 ...

  3. Android - Resource 之 String 小结

    简单的string: <?xml version="1.0" encoding="utf-8"?> <resources> <st ...

  4. debug代码时遇到循环时提高效率方法

    在循环时,需要查看uid=uid=12007255时的代码执行情况 可以在循环代码中加入 if((Long)map.get("uid") == 12007255){ System. ...

  5. kali linux 64bit 2019.1a下启动bbqsql:No module named coros

    kali linux 64bit 2019.1a下bbqsql启动失败,错误: File "/usr/local/lib/python2.7/dist-packages/bbqsql/lib ...

  6. django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...

  7. 新建一个Windows Service的方法

    http://www.cnblogs.com/YanPSun/archive/2010/05/22/1741381.html http://blog.csdn.net/m15188153014/art ...

  8. 振兴中华|2013年蓝桥杯A组题解析第三题-fishers

    标题: 振兴中华 小明参加了学校的趣味运动会,其中的一个项目是:跳格子. 地上画着一些格子,每个格子里写一个字,如下所示:(也可参见p1.jpg) 从我做起振 我做起振兴 做起振兴中 起振兴中华 比赛 ...

  9. POJ1741 Tree(树分治——点分治)题解

    题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...

  10. 【做题】HDU6331 Walking Plan——矩阵&分块

    题意:给出一个有\(n\)个结点的有向图,边有边权.有\(q\)组询问,每次给出\(s,t,k\),问从\(s\)到\(t\)至少经过\(k\)条边的最短路. \(n \leq 50, \, q \l ...