当前位置:网站首页>P3743 kotori的设备
P3743 kotori的设备
2022-07-26 08:32:00 【Tiredd】
题目背景
kotori 有 nn 个可同时使用的设备。
题目描述
第 i 个设备每秒消耗 ai 个单位能量。能量的使用是连续的,也就是说能量不是某时刻突然消耗的,而是匀速消耗。也就是说,对于任意实数,在 k 秒内消耗的能量均为 k*ai 单位。在开始的时候第 i 个设备里存储着 bi 个单位能量。
同时 kotori 又有一个可以给任意一个设备充电的充电宝,每秒可以给接通的设备充能 p 个单位,充能也是连续的,不再赘述。你可以在任意时间给任意一个设备充能,从一个设备切换到另一个设备的时间忽略不计。
kotori 想把这些设备一起使用,直到其中有设备能量降为 0。所以 kotori 想知道,在充电器的作用下,她最多能将这些设备一起使用多久。
输入格式
第一行给出两个整数 n,pn,p。
接下来 nn 行,每行表示一个设备,给出两个整数,分别是这个设备的ai 和 bi。
输出格式
如果 kotori 可以无限使用这些设备,输出 -1。
否则输出 kotori 在其中一个设备能量降为 0 之前最多能使用多久。
输入样例:
2 1 2 2 2 1000
输出样例:
2.0000000000
输入样例:
1 100 1 1
输出样例:
-1
思路:问的是求最长需要多久,那么可以从二分或者dp的方向去思考(其实就是因为标题是二分....),我们去二分答案,每次二分出来一个t。然后思考答案左边区间和右边区间会满足怎样的条件。这里显然,如果对于一个机器,他的ai * t <= bi,那么说明在t时间内,该机器不需要充电。反之需要重点ai * t - bi,那么看看t时间内充电宝是否能满足全部机器即可。如果满足,则说明答案在mid ~ r,反之说明在l ~ mid。
code如下:
#include <iostream>
using namespace std;
const int N = 100010;
int n, q;
double a[N], b[N], s;
bool cheak(double t)
{
double p = t * q, sum = 0;
for(int i = 1; i <= n; i ++ )
if(a[i] * t > b[i]) sum += a[i] * t - b[i];
return sum <= p;
}
int main()
{
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i ++ ) scanf("%lf%lf", &a[i], &b[i]), s += b[i];
if(q >= s) printf("-1\n");
else
{
double l = 0, r = 1e10;
while(r - l > 1e-5)
{
double mid = (l + r) / 2;
if(cheak(mid)) l = mid;
else r = mid;
}
printf("%.10lf", r);
}
return 0;
}边栏推荐
- 利用模m的原根存在性判断以及求解
- 2022-7-5 personal qualifying 2 competition experience
- Memory management - dynamic partition allocation simulation
- Flutter compilation fails
- [endnote] detailed explanation of document template layout syntax
- 第四天作业
- 23.6 23.7 web environment web environment variable reading
- 基于Raft共识协议的KV数据库
- KV database based on raft consensus protocol
- Uninstallation of dual systems
猜你喜欢

Write common API tools swagger and redoc

Why reserve a capacitor station on the clock output?

OSPF summary

基于Raft共识协议的KV数据库

Nodejs2day (modularization of nodejs, NPM download package, module loading mechanism)

Spark persistence strategy_ Cache optimization
![[time complexity, space complexity]](/img/f2/f82c7e0a6ab9f893023c2ddbac3431.png)
[time complexity, space complexity]

为什么要在时钟输出上预留电容的工位?

Winter vacation homework & Stamp cutting

Mysql8 dual master and dual slave +mycat2 read / write separation
随机推荐
flex三列布局
2022-7-4 personal qualifying 1 competition experience
[GUI] GUI programming; AWT package (interface properties, layout management, event monitoring)
2022-7-5 personal qualifying 2 competition experience
内存管理-动态分区分配方式模拟
Condition judgment function of MySQL function summary
Redis进阶
JS tool function Encyclopedia
第四天作业
Using the primitive root of module m to judge and solve
Does flinkcdc now support sqlserver instance name connection?
matplotlib学习笔记
Day 3 homework
Winter vacation homework & Stamp cutting
Data validation typeerror: qiao Validate is not a function
2022年全国职业院校技能大赛“网络安全”竞赛试题文件上传渗透测试答案Flag
【EndNote】文献模板编排语法详解
Flutter WebView jitter
请问flink sql client 在sink表,有什么办法增大写出速率吗。通过sink表的同步时
Sed job