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 memory limit per test 256 megabytes input standard input output standard outputYou 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.
InputThe 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.
OutputIf 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 input2 1output
2 2
2 1000
2.0000000000input
1 100output
1 1
-1input
3 5output
4 3
5 2
6 1
0.5000000000Note
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
ps1:二分这种小数选用规定二分100次(2^100)或者更多即可,尽量不要用while(fabs(r-l)>eps)
ps2:二分上线r最少应该是1e10,不然会被hack(比赛结束后看其他人讨论时才知道这个hack点的,一阵后怕,还好我刚好写的是1e10)
#include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB push_back typedef long long LL; typedef pair<int,int> PII; const double eps=1e-8; const double pi=acos(-1.0); const int K=1e6+7; const int mod=1e9+7; int n,p,a[K],b[K]; double ans; bool check(double x) { double sum=0; for(int i=1;i<=n;i++) { sum+=max(a[i]*x-b[i],0.0); if(sum>=x*p+1e-10) return 0; } return 1; } int main(void) { cin>>n>>p; for(int i=1;i<=n;i++) scanf("%d%d",a+i,b+i); double l=0,r=1e10; for(int i=1;i<=100;i++) { double mid=(l+r)/2.0; if(check(mid)) ans=mid,l=mid; else r=mid; } if(fabs(ans-1e10)<=1e-8) printf("-1\n"); else printf("%.8f\n",ans); return 0; }
- 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)A. Vicious Keyboard
- CF-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 #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake二分
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) 题解(待续)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E DNA Evolution
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) D. Best Edge Weight(最小生成树+LCA+树链剖分)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组
- Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow
- Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) E. Down or Right
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) C. Success Rate
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C 并查集
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)C. Jury Marks