Codeforces Round #409 C Voltage Keepsake(二分)
2017-04-17 10:36
405 查看
题意:有n个仪器,每个仪器每秒有a[i]消耗,自身有b[i]的存储,你有一个充电器,可以每秒充P的电,当某个仪器电量为0的时候就全部停止,问最大运行时间
思路:显然的二分一下答案嘛,然后check一下就可以了
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
output
input
output
input
output
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.
思路:显然的二分一下答案嘛,然后check一下就可以了
#include<bits/stdc++.h> using namespace std; #define LL long long const int maxn = 1e5+7; int n,p,a[maxn],b[maxn]; LL tot; bool check(double ans) { double now = ans; for(int i = 1;i<=n;i++) if(b[i]-a[i]*ans<0) now+=(b[i]-a[i]*ans)/p; return now>=0; } int main() { scanf("%d%d",&n,&p); for(int i = 1;i<=n;i++)scanf("%d%d",&a[i],&b[i]),tot+=a[i]; if(p>=tot) { printf("-1\n"); return 0; } double l = 0,r = 1e18; for(int i = 0;i<100;i++) { double mid = (l+r)/2; if(check(mid))l=mid; else r = mid; } printf("%.10lf\n",(l+r)/2); }
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
2 1 2 2 2 1000
output
2.0000000000
input
1 100 1 1
output
-1
input
3 5 4 3 5 2 6 1
output
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.
相关文章推荐
- Codeforces Round # 409 C. Voltage Keepsake (二分)
- Codeforces Round # 409 C. 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 Round #271 (Div. 2) B Worms 裸的二分查找
- Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
- Educational Codeforces Round 21 D ( 二分 )
- Educational Codeforces Round 11 C. Hard Process(尺取 二分 区间天数最多
- Codeforces Round #377 (Div. 2) D 二分
- Codeforces Round #283 Div.2 D Tennis Game --二分
- Codeforces Round #350 (Div. 2) D2 Magic Powder - 2【二分】
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- Educational Codeforces Round 11 C. Hard Process(尺取 二分 区间天数最多
- Codeforces Round #400 C. Molly's Chemicals (二分)
- (二分)Codeforces Round #415 (Div. 2) D-Glad to see you!
- Codeforces Round #376 (Div. 2) F. Video Cards (二分)(lower bound)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #202 (Div. 1) A. Mafia(二分,思路)
- Codeforces Round #278 (Div. 2) C. Fight the Monster 二分+枚举