Codeforces 801C - Voltage Keepsake
2 seconds
256 megabytes
standard input
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.
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.
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
.
2 1
2 2
2 1000
2.0000000000
1 100
1 1
-1
3 5
4 3
5 2
6 1
0.5000000000
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.
题目大意:有n个装备,每个设备耗能为每单位时间耗能ai,初始能量为bi;你有一个充电宝,每单位时间可以冲p能量,你可以在任意时间任意拔冲。
如果可以所有设备都可以一直工作下去,输出-1;否则,输出所有设备都同时工作的最长时间。
思路提示:想象这样一个场景,每当一个设备没电时,你就拔掉你正在充电的设备,冲到这个设备上。可是,天有不测风云,突然某一刻,有2个以上设备同时没电,那至少有一个设备得停止工作。这一刻也就是答案,让我们来看一看这一刻的状况,设这一刻为t,充电宝总共供能t*p,这时t*p==sum(a[i]*t)-sum(b[i]);当t*p<sum(a[i]*t)-sum(b[i])说明t比答案大;当t*p>sum(a[i]*t)-sum(b[i])说明,t比答案小。
方法:在实数域上二分,不断逼近答案。
代码1:
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
const int N=1e5+;
const double eps=1e-;//精度
int n,p;
int a[N],b[N];
int check(double mid)
{
double E=mid*p;
for(int i=;i<n;i++)
{
double e=b[i]-a[i]*mid;
if(e<)E+=e;
if(E<)return ;//如果充电宝的能量不足以供应,说明mid太大
}
return ;//否则mid太小
}
int main()
{
cin>>n>>p;
ll sum=;
for(int i=;i<n;i++)
{
cin>>a[i]>>b[i];
sum+=a[i];
}
if(sum<=p)
{
cout<<-<<endl;
return ;
}
double low=,high=1e18;
double mid;
while(high-low>=eps)//实数域上的二分不可能为0,只能以精度控制
{
mid=(high+low)/;
if(check(mid))low=mid;
else high=mid;
}
cout<<mid<<endl;
return ;
}
代码2:
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const int N=1e5+;
const double eps=1e-;
int a[N],b[N];
int main()
{
int n,p;
cin>>n>>p;
ll sum=;
for(int i=;i<n;i++)
{
cin>>a[i]>>b[i];
sum+=a[i];
}
if(sum<=p)
{
cout<<-<<endl;
return ;
}
double l=,r=1e18;
while(r-l>=eps)
{
double mid=(r+l)*0.5;
double s=;
for(int i=;i<n;i++)
{
if(b[i]-a[i]*mid<)s+=a[i]*mid-b[i];
}
if(s>mid*p)r=mid;
else l=mid;
}
cout<<(r+l)*0.5<<endl;
return ;
}
Codeforces 801C - Voltage Keepsake的更多相关文章
- Codeforces 801C Voltage Keepsake(二分枚举+浮点(模板))
题目链接:http://codeforces.com/contest/801/problem/C 题目大意:给你一些电器以及他们的功率,还有一个功率一定的充电器可以给这些电器中的任意一个充电,并且不计 ...
- Codeforces 772A Voltage Keepsake - 二分答案
You have n devices that you want to use simultaneously. The i-th device uses ai units of power per s ...
- CodeForces 772A Voltage Keepsake
二分答案,验证. 二分到一个答案,比他小的时间都需要补充到这个时间,计算所需的量,然后和能提供的量进行比较. #include <cstdio> #include <cmath> ...
- Voltage Keepsake CodeForces - 801C (贪心 || 二分)
C. Voltage Keepsake time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- 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 ...
- 【codeforces 801C】Voltage Keepsake
[题目链接]:http://codeforces.com/contest/801/problem/C [题意] 有n个设备 你想同时使用 第i个设备每分钟消耗ai点电量,一开始有bi点电量 你有一个充 ...
- Voltage Keepsake CodeForces - 801C (思维+二分)
题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...
- Voltage Keepsake CodeForces - 801C(思维)
题意: 有n台机器,第i台机器每个单位时间消耗ai的功率,初始有bi的功率储备,有一个充电器每个单位时间充p单位的功率 问经过多长时间才能有一个功率位0的机器,如果能够无限使用输出-1: 解析: 时间 ...
随机推荐
- Spring Boot 2(一):Spring Boot 2.0新特性
Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...
- centos 6.5 gdb 7.10安装make[5]: *** [install-bfdincludeHEADERS] Error 1解决
make[5]: *** [install-bfdincludeHEADERS] Error 1make[5]: Leaving directory `/usr/local/gdb-7.10/bfd' ...
- 维护keepalived与mysql漂移脚本
环境拓扑 chengAlived #!/bin/bash function checkModelone(){ echo "重新进行获取" wget 192.168.158.147: ...
- applyColorMap()研究(如果我对现有的colormap不满意,那么如何具体来做)
cv::applyColorMap()能够实现预定义的伪彩色,这个是众所周知的事情. 并且和matlab提供的很相近 除了这些预置的变换,如果我想实现新的变换,需要做LUT变换 cv::Mat ...
- Codeforces 844D Interactive LowerBound - 随机化
This is an interactive problem. You are given a sorted in increasing order singly linked list. You s ...
- topcoder srm 325 div1
problem1 link $g[i]$表示解决前$i$个的代价,那么$g[i]$是所有$g[j]+cost(j+1,i)$的最小值. import java.util.*; import java. ...
- Spring Boot 项目初始化
Spring Boot 项目创建 File->New->New Project->Spring Initializr 勾选 Web Spring Boot 版本选择稳定版,本文选择 ...
- IntelliJ IDEA 中SpringBoot对Run/Debug Configurations配置 SpringBoot热部署
运行一个SpringBoot多模块应用 使用SpringBoot配置启动: Use classpath of module选中要运行的模块 VM options:内部配置参数 -Dserver.por ...
- Newcoder 华华给月月出题(线筛)题解
题目描述: 华华刚刚帮月月完成了作业.为了展示自己的学习水平之高超,华华还给月月出了一道类似的题: Ans=⊕Ni=1(iNmod(109+7))Ans=⊕i=1N(iNmod(109+7)) ⊕⊕符 ...
- LightOJ 1030 Discovering Gold(概率DP)题解
题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...