Codeforces Round # 409 C. Voltage Keepsake (二分)
2017-04-17 18:16
453 查看
题意
给定 n 台机器同时使用,第 i 台机器每秒消耗能量 ai ,初始能量为 bi 。有一个充电器能给 n 台机器充电,同一时间只能给一台机器充电,每秒充能量 p 。其中充电不必按整秒进行,即例如给第 1 台充 0.001 秒,给第 2 台充 0.12 秒是合法的,且不必考虑交换充电机器的时间。问 n 台机器最多能同时工作多少时间?若 n 台机器能够无限工作,输出 -1 。
思路
-1的情况是很好判断的。我们猜测这样一个结论:如果一个时间t能够满足题目要求,那么任意比t小的实数都能满足要求。
这样时间就满足二分性,可以对时间进行二分,对于一个给定的时间求提供的能量是否能满足所有电器的需求。
hack点:
二分的上界较大
二分精度存在一定误差
eps过小可能超时
由于题目要求相对精度在1e-6,我们其实只需要限制迭代次数就好了,当然用eps也是可以的。
代码
#include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define rep(i,a,b) for(int i=a;i<b;i++) const int INF=0x3f3f3f3f; const int maxn=1e5+50; const int mod=1e9+7; const double eps=1e-8; #define pii pair<int,int> typedef long long ll; typedef unsigned int ui; using namespace std; int n; int p; int a[maxn],b[maxn]; double calPower(double t){ double tot=t*p; rep(i,0,n){ double tmp=b[i]-a[i]*t; if(tmp<0) tot+=tmp; } return tot; } int main() { #ifndef ONLINE_JUDGE //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif //int T; scanf("%d",&T); while(~scanf("%d %d",&n,&p)){ ll tot=0; rep(i,0,n) { scanf("%d %d",&a[i],&b[i]); tot+=a[i]; } if(p>=tot){ puts("-1"); continue; } double tl=0,tr=1e18; while(tr-tl>eps){ double mid=(tr+tl)/2; //cout<<mid<<" "<<calPower(mid)<<endl; double tmp=calPower(mid); if(tmp>0) tl=mid; else tr=mid; } printf("%.8lf\n",tl); } return 0; }
相关文章推荐
- 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 #409 (rated, Div. 2, based on VK Cup 2017 Round 2)A. Vicious Keyboard
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
- Codeforces Round #283 (Div. 2) D. Tennis Game 二分
- Codeforces Round #350 (Div. 2) D Magic Powder (二分)
- 【Codeforces Round 276 (Div 2)D】【数论 正难则反 枚举倍数 二分 O(nlogn)】Maximum Value n个数大数mod小数找最大余数
- Codeforces Round #377 (Div. 2) D. Exams 二分+贪心 or 纯贪心水过
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心
- Codeforces Round #202 (Div. 1) A. Mafia二分查找,玩游戏
- Educational Codeforces Round 23 D. Imbalanced Array(单调栈或RMQ+二分)
- Codeforces Round #327 (Div. 2) D. Chip 'n Dale Rescue Rangers 二分 物理
- Codeforces Round #330 (Div. 2)D. Max and Bike 二分 物理
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
- Codeforces Round #398(Div. 2)D. Cartons of milk【二分+暴力处理】
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
- Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)
- Codeforces Round #384 (Div. 2)E(状压dp+二分,好题)