您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐