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. T-shirt buying CodeForces - 799B (小根堆+STL)

    题目链接 思路: 由于题目说了只有1,2,3,三种色号的衣服,然后开三个对应色号的小根堆, 我是根据pair<int,int> 创建了一个以价格小的优先的优先队列. pair中的另外一个i ...

  2. Python_服务器与多客户端通信、UDP协议、pycharm打印带颜色输出、时间同步的机制

    1.服务器与多客户端通信 import socket # 创建tcp socket的套接字 sk = socket.socket() # bind sk.bind(('127.0.0.1',8080) ...

  3. 后台管理系统之邮件开发(Java实现)

    一,功能点 后台管理系统,添加用户时.对注册的新用户邮箱发送初始密码. 二,代码实现 1.Mail实体类 public class Mail { private Set<String> r ...

  4. Golang开发工具LiteIDE使用方法整理

    安装 参考github的说明 添加GOPATH 创建workspace 创建新文件 运行程序 Liteide中运行程序有两种方式: FR(FileRun)是编译并运行单个文件,可以使用Shift + ...

  5. Vmware的虚拟机示例进入BIOS方法

    虚拟机(Vmware)怎么进入BIOS_百度经验 https://jingyan.baidu.com/article/7e440953e566472fc0e2eff7.html Vmware虚拟机进入 ...

  6. 如何使用RSS

    (转载: http://www.ruanyifeng.com/blog/2006/01/rss.html) 一. 自从我发现很多人不知道什么是RSS以后,我就一直想向大家介绍它,因为它太有用了,将来会 ...

  7. myeclipse部署报错报空指针异常

    hib4.1+spring3+struts2项目 项目运行报错,把WEB-INF/classes目录删除后,想再重新编译并自动部署.再自动部署时总是提示错误: Errors occurred duri ...

  8. Tomcat 目录结构以及基本配置

    1 Tomcat 目录层次结构 ① bin:存放启动和关闭tomcat 的脚本文件② conf: 存放配置文件 server.xml:该文件用于配置和server 相关的信息,比如tomcat 启动端 ...

  9. Day 4-1 模块的导入方法和路径

    什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...

  10. 剑指offer(14)

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 思路: 这里有个细节,我们发现,6节点的子节点在操作之后并没有发生变化,所以等会我们在交换的时候,交换的不是节点的数值,而是整个节点. 另外我们进 ...