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

Copy
2 1
2 2
2 1000
output

Copy
2.0000000000
input

Copy
1 100
1 1
output

Copy
-1
input

Copy
3 5
4 3
5 2
6 1
output

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

这题网上大多都用的二分

先用贪心做一遍

贪心的做法是 先用初始使用时间排序 然后 如果前i个在边充电边使用的情况下  能达到(i + 1)的使用时间 那就把前(i + 1)合并为一个仪器

然后再判断前(i + 1)个在边充电边使用的情况下 能否使用时间达到 第(i + 2)个

#include <bits/stdc++.h>
using namespace std;
const int maxn = , INF = 0x7fffffff;
typedef long long LL; struct node
{
double t, a, b;
}Node[maxn]; double cmp(node A, node B)
{
return A.t < B.t;
} int main()
{
int n, p;
LL sum = ;
cin >> n >> p;
for(int i = ; i < n; i++)
{
cin >> Node[i].a >> Node[i].b;
sum += Node[i].a;
Node[i].t = Node[i].b / (double) Node[i].a;
} if(p >= sum) return puts("-1"), ; //只有在充电的功率大于消耗的功率的时候 才能无限使用 sort(Node, Node + n, cmp); //按初始使用时间排序
double pow = Node[].t * p, x_p = Node[].a, ret = ;
bool flag = ;
int i;
for(i = ; i < n - ; i++)
{
if(pow + (Node[i + ].t - Node[i].t) * p > (Node[i + ].t - Node[i].t) * x_p)
{
pow += (Node[i + ].t - Node[i].t) * (p - x_p), x_p += Node[i + ].a;
}
else
{
break;
}
}
ret = pow / (double) (x_p - p); //存的功率 除 净消耗功率 是不是就是使用时间
ret += Node[i].t; //然后再加上初始的使用时间
printf("%.10f\n", ret); return ;
}

二分 :

#include <bits/stdc++.h>
#define eps 1e-9
using namespace std;
const int maxn = , INF = 0x7fffffff;
typedef long long LL;
int n, p;
int a[maxn], b[maxn]; double check(double m)
{
double sum = ;
for(int i = ; i < n; i++)
{
if(b[i] - a[i] * m < )
sum += a[i] * m - b[i];
}
return p * m > sum;
} int main()
{
LL sum_a = , sum_b = ;
cin >> n >> p;
for(int i = ; i < n; i ++)
{
cin >> a[i] >> b[i];
sum_a += a[i];
}
if(p >= sum_a) return puts("-1"), ; double l = , r = 1e18, t, m;
while(r - l > eps)
{
m = (l + r) / (double);
if(check(m)) l = m;
else r = m;
}
printf("%.10f\n", m); return ;
}

Voltage Keepsake CodeForces - 801C (贪心 || 二分)的更多相关文章

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

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

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

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

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  4. Codeforces 801C - Voltage Keepsake

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

  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. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

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

  8. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  9. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

随机推荐

  1. 200 ok 几种状态

    浏览器加载资源成功一般会有几种状态 200 ok   ----  从原始服务器请求成功 200 ok from cache    ---- 200 ok from disk cache  ---- 2 ...

  2. hibernate延迟加载org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.javakc.hibernate.onetomany.entity.DeptEntity.emp, could not initialize proxy - no Session

    public static void main(String[] args) {  DeptEntity dept = getDept("402882e762ae888d0162ae888e ...

  3. Telnet服务器和客户端请求处理

    Telnet服务器和客户端请求处理 本文的控制台项目是根据SuperSocket官方Telnet示例代码进行调试的,官方示例代码:Telnet示例. 开始我的第一个Telnet控制台项目之旅: 创建控 ...

  4. 【学习总结】Git学习-参考廖雪峰老师教程三-创建版本库

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  5. 实验楼----PHP代码审计(sha1、md5)

    地址:http://www.shiyanbar.com/ctf/1787 题目:

  6. C调用C++, C++调用C方法

    1. C 调用 C++封装好后的函数: -> 在C++中有一个函数 int main_cpp(): -> 首先构建头文件, #ifndef CPP_FILE_H   #define CPP ...

  7. composer更改源为国际

    composer config -g repo.packagist composer https://repo.packagist.org

  8. laravel添加model文件夹,需要改动的地方

    首先,将app\User(等model文件),移入APP\modellists文件夹中,方便整理 第二,修改模型中命名空间和引用其他model的路径 第三,将文件夹app\admin中的控制器文件,全 ...

  9. ES6/ES2015的一些特性的简单使

    1.一些常用的ES6的特性: let, const, class, extends, super, arrow functions, template string, destructuring, d ...

  10. JS 类型检测

    typeof 适合函数对象和基本类型的判断 typeof 100instanceof 适合判断对象类型 obj instanceof Object 基于原型链判断操作符,若做操作符不是对象,则会直接返 ...