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


  二分答案,然后判断所有需要充电的需要充的电是否大于等于充电总量。

  注意初值和控制一下二分的次数。

Code

 /**
* Codeforces
* Problem#772A
* Accepted
* Time:61ms
* Memory:2836k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} inline int dcmp(double a, double b) {
if(fabs(a - b) < eps) return ;
if(a - b < ) return -;
return ;
} int n, p;
int *a, *b;
long long s = ; inline void init() {
readInteger(n);
readInteger(p);
a = new int[n + ];
b = new int[n + ];
for(int i = ; i <= n; i++) {
readInteger(a[i]);
readInteger(b[i]);
s += a[i];
}
} boolean check(double mid) {
double cost = 0.0;
for(int i = ; i <= n; i++)
if(a[i] * mid >= b[i])
cost += a[i] * mid - b[i];
return cost < p * mid;
} inline void solve() {
if(s <= p) {
puts("-1");
return;
}
double l = , r = 1e16;
int times = ;
while(dcmp(l, r) == - && times < binary_limit) {
double mid = (l + r) / ;
times++;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.6lf", l);
} int main() {
init();
solve();
return ;
}

Codeforces 772A Voltage Keepsake - 二分答案的更多相关文章

  1. CodeForces 772A Voltage Keepsake

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

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

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

  3. [Codeforces 1199C]MP3(离散化+二分答案)

    [Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...

  4. Codeforces 801C - Voltage Keepsake

    C. Voltage Keepsake 题目链接:http://codeforces.com/problemset/problem/801/C time limit per test 2 second ...

  5. 2018.12.08 codeforces 939E. Maximize!(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...

  6. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  7. CodeForce-801C Voltage Keepsake(二分)

    题目大意:有n个装备,每个设备耗能为每单位时间耗能ai,初始能量为bi;你有一个充电宝,每单位时间可以冲p能量,你可以在任意时间任意拔冲. 如果可以所有设备都可以一直工作下去,输出-1:否则,输出所有 ...

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

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

  9. 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 ...

随机推荐

  1. [转帖]unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)

    觉得这个讲的挺好的,就转过来了:) 太抽象的理论总是让人眼花缭乱,所以我这里以例证为主. 1,测试OnTriggerEnter和OnCollisionEnter的区别 测试:如果两个物体A,B 两者都 ...

  2. c#中枚举类型的定义与使用

    介绍枚举是一个指定的常数,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.定义默认基数从O开 ...

  3. hive javaapi 002

    默认开启10000端口开启前,编辑hive-site.xml设置impersonation,防止hdfs权限问题,这样hive server会以提交用户的身份去执行语句,如果设置为false,则会以起 ...

  4. html2pdf后逐页固定位置盖公章

    需要通过NuGet添加Html2Pdf引用 #region 通过Html来生成pdf,测试Html样式 /// <summary> /// 通过Html来生成pdf,测试Html样式 // ...

  5. tfs团队项目删除原来连接的默认账户

    1.在用visual studio 连接团队项目时,首次输入用户名和密码后,默认保存住凭据了,等以后连接会自动采用首次的凭证. 但是如何采用新的用户重新登录呢.如图所示,删除原有的凭证.删除后重启电脑 ...

  6. Unity shader学习之Alpha Test的阴影

    Alpha Test的阴影, shader如下: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClip ...

  7. Vue系列之 => 结合ajax完成列表增删查

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. hdu5032 树状数组

    题意: 对于一个1000*1000的Mushroom, 起点在(1,1)给定一个斜率和一个x,求由斜率和x所对应的直线构成的三角形内蘑菇的总值. 每个点的对应的值为(x+A)(y+B) 解 每个点都有 ...

  9. codeforces 980A Links and Pearls

    题意: 有珍珠和线,问能否重新安排使得相邻珍珠之间的线的数量相等. 思路: 首先,珍珠为0或者线为0,那么都满足条件: 其次,如果珍珠的个数大于线的个数,那么肯定不满足条件: 然后,如果线的个数能够被 ...

  10. 设计模式之Singleton(单态)(转)

    定义: Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作. 还有, singleton能够被状 ...