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. 美团面试(c++方向)

    美团后台基本都是java,c++很少的 一面:(其实问了很多,但是很多不记得了) 1.    mfc里边的句柄, Qt里边的信号和槽函数 2.    c 程序的内存分布 3.    堆和栈的区别 4. ...

  2. plw的晚餐(毒瘤题害我暴0)

    题意 描述 plw吃完午饭之后,马上又觉得肚子饿了.他决定马上从美食区离开,赶往下一个吃饭地点"香香鸡".但是在plw离开离开美食区之前,需要按美食区的规矩画一个特殊符号,并且如果 ...

  3. R语言绘制茎叶图

    与直方图相比,茎叶图更能细致的看出数据分布情况! 代码: > x<-c(25, 45, 50, 54, 55, 61, 64, 68, 72, 75, 75,+ 78, 79, 81, 8 ...

  4. python的循环和选择

    一.python的选择结构: python的选择结构有两种选择结构一种是单选择(if...else)另一种则是多选择结构(if ...elif...elif) 下面用代码来实现: 1.if....el ...

  5. Effective java ---遵守普遍接受的命名规则

    alibaba的java命名规范如下: . [强制]代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ ...

  6. (关于数据传输安全)SSH协议

    这里说的不是java的SSH框架,是1995年,芬兰学者Tatu Ylonen设计的SSH协议. 有计算机网络基础的同学都知道,在网上传输的数据是可以被截取的.那么怎样才能获得安全? 一.春点行话 电 ...

  7. JS刷新当前页面的几种方法总结

    reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页 ...

  8. linux和sqlserver 2017的安装

    这两天一直在弄linux的安装过程.中间也遇到了不少的坑,主要是网络上的坑人的文章太多.都是坑,最后从redhat官网下载了iso文件,顺便看到官网推荐了一个fedora media writer的烤 ...

  9. php trait使用

    trait类似于基类  同样的方法优先级为 本类>trait>基类 <?php /** * Created by PhpStorm. * User: mac * Date: 2019 ...

  10. day 7-6 GIL,死锁,递归锁与信号量,Event,queue,

    摘要: 1.死锁与递归锁 2.信号量 3.Event 4.Timer 5.GIL 6.Queue 7.什么时候该用多线程和多进程 一. 死锁与递归锁 所谓死锁: 是指两个或两个以上的进程或线程在执行过 ...