Codeforces Round #409C Voltage Keepsake(二分)
来源:互联网 发布:it软件测试培训班 编辑:程序博客网 时间:2024/06/03 18:13
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 12 22 1000
2.0000000000
1 1001 1
-1
3 54 35 26 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.
这样的连续时间实在想不起来贪心咋做,他既然可以在任意时刻充电那么就不关心何时给谁充电了,既然牵扯最大时间,用二分枚举时间即可。
打印-1的唯一条件是 用电赶不上冲的快,即sum(a[i])<= p。这里二分的右值应尽量大一些。
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<map>#include<cmath>using namespace std;#define mem(a,b) memset(a,b,sizeof(a))typedef long long ll;const int maxn = 1e5+5;const int ff = 0x3f3f3f3f;const double esp = 1e-8;struct node{int a;int b;} v[maxn];int n,p;int main(){cin>>n>>p;ll sum = 0;for(int i = 1;i<= n;i++){scanf("%d %d",&v[i].a,&v[i].b);sum+= v[i].a;}if(sum<= p){cout<<-1<<endl;return 0;}double l = 0,r = 1e10+1;while(r-l>= esp){double mid = (r+l)/2;double tmp = p*mid;for(int i = 1;i<= n;i++){if(mid*v[i].a*1.0 - v[i].b*1.0> 0)tmp-= (mid*v[i].a - v[i].b*1.0);}if(tmp< 0)r = mid;elsel = mid;}printf("%.6f\n",l);return 0;}
- Codeforces Round #409C Voltage Keepsake(二分)
- Codeforces Round # 409 C. Voltage Keepsake (二分)
- Codeforces Round #409 Div.2(C. Voltage Keepsake)二分
- Codeforces Round #409 C Voltage Keepsake(二分)
- Codeforces Round # 409 C. Voltage Keepsake (二分)
- Codeforces Round #409 (Div. 2) C. Voltage Keepsake 二分
- Codeforces 801C Voltage Keepsake 二分+精度
- Codeforces 801C Voltage Keepsake【二分】
- codeforces 801C Voltage Keepsake 二分答案
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C. Voltage Keepsake
- Codeforces 801C Voltage Keepsake
- Codeforces 801C Voltage Keepsake
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C-Voltage Keepsake
- codeforces C. Voltage Keepsake (浮点数二分坑点控制精度可解决)
- C. Voltage Keepsake
- C. Voltage Keepsake
- Voltage Keepsake(二分答案)
- Logistic Regression 算法学习
- 比iPhone8更重要的新App Store,给内容产品的未来指出明路
- 新零售行业的战略管理方法论
- 一张图讲清楚产品架构,手把手教你画产品框架图
- 一位 CEO 对 CTO 技术领导力的期待是什么?
- Codeforces Round #409C Voltage Keepsake(二分)
- break与continue的区别
- Pinterest 首位产品经理:爆发式增长背后的 5 大经验总结
- PTA 6-1 顺序表创建和就地逆置
- 如何挖掘消费者的隐性需求?
- 史上最详细的客服系统产品落地|后台产品经理的工作实例,有那么苦吗?
- 长假后第一天不想工作,这病咋治?
- 干货整理及PPT下载福利 2017(上海)人工智能产品经理大会
- 动态规划