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. RabbitMQ教程(二) ——linux下安装rabbitmq

    安装过程参考官网: Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) 首先需要安装erlang,参考:http://fedo ...

  2. [LeetCode] 56 - Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. Linux提示删除文件cannot remove `文件名': Operation not permitted

    Linux系统下删除某个文件时提示如下报错: 执行lsattr命令可以看到隐藏属性-------i--------,如下图: 通过查找资料发现: chattr命令用于改变文件属性.这项指令可改变存放在 ...

  4. ajax相关问题

    1.contentType和dataType contentType 主要设置你发送给服务器的数据格式 dataType设置你收到服务器数据的格式(如text,json等),最常用的为json. 2. ...

  5. Individual Project

    这次我自己完成了一个小小的项目,课可以把这篇随笔当做一次实验报告,主要的内容是用JUnit进行单元测试.由于我的技术太弱了,就在博客园里“求师”,按照大神的方法慢慢把这些东西写了下啦来. 不知道怎么搞 ...

  6. MySQL中关于数据类型指定宽度之后的情况

    概述 MySQL有很多种数据类型,最常用的就是int,char,varchar,这些类型在创建表的时候都可以指定该字段的宽度,方法是在类型后面加一个括号,括号中写宽度就可以了. 但是,在指定宽度之后, ...

  7. 【kindle笔记】之 《犬夜叉》-2017-12-26

    [kindle笔记]读书记录-总 2017-12-26 <犬夜叉> 买kindle的初衷是看计算机工具书看得眼快瞎了,我弟弟推荐给我的Linux系列<鸟叔私房菜> 真的是深思熟 ...

  8. 解决selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

    解决方案: 1.查看浏览器当前版本:chrome://version/. 2.到https://sites.google.com/a/chromium.org/chromedriver/downloa ...

  9. CodeForces Round #550 Div.3

    http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...

  10. 【Python3练习题 009】 打印出所有的“水仙花数”

    # [Python练习题 009] 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,# 其各位数字立方和等于该数本身.例如:153是一个“水仙花数”,# 因为153=1的三次方+5的三次方+ ...