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. 【转】mysql热备

    mysql双机热备的实现 亲测可用

  2. 小小知识点(一)——利用电脑自带的BitLocker对磁盘加密

    1.利用电脑自带的BitLocker可以对固定的或移动的磁盘加密 网上有很多的使用方法步骤,可参考百度经验:https://jingyan.baidu.com/article/636f38bb4fac ...

  3. Elasticsearch 5.0Head插件

    Elasticsearch 5.0 —— Head插件部署指南   使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方 ...

  4. MySQLl导入导出SQL文件

    window 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u dbuser -p dbname > dbname.sql ...

  5. 现代程序设计 homework-06

    写代码爽还是读代码爽? 当然是写代码爽好吧... 读代码明显是读+写两倍的工作量好么... 本次作业要求: 1) 把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能 ...

  6. [2017BUAA软工助教]案例分析小结

    BUAA案例分析小结 一.作业要求 http://www.cnblogs.com/jiel/p/7631784.html 二.统计数据 总人数 神策数据 博客园博客 必应词典 30 1 12 17 三 ...

  7. java开发中使用枚举表述数据字典

    一.用枚举表述数据字典 1.代码: package com.inspire.jdk.caculate; /** * Created by yaming * 用枚举表述常量数据字段 */ public ...

  8. PHP中多个文件包含的问题 (二)

    首先php中有常用的两种方法将文件包含:include和require,而include_once和require_once无非就是升级版而已,这里就不阐述他们的区别,我只提一下我遇到的问题: 先看一 ...

  9. 解决scrapy报错:ModuleNotFoundError: No module named 'win32api'

    ModuleNotFoundError: No module named 'win32api' 表示win32api未安装 解决办法: 下载对应python版本的win32api,并安装. 下载地址: ...

  10. Nginx Configuring HTTPS servers

    Configuring HTTPS servershttp://nginx.org/en/docs/http/configuring_https_servers.html Configuring HT ...