A. Voltage Keepsake(二分枚举)

https://codeforces.com/problemset/problem/772/A


 

输入输出样例

输入 #1复制

2 1
2 2
2 1000

输出 #1复制

2.0000000000

输入 #2复制

1 100
1 1

输出 #2复制

-1

输入 #3复制

3 5
4 3
5 2
6 1

输出 #3复制

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/52/5 of a second, then switch to charge the second device for a 1/101/10 of a second.


思路:比较明显的二分.check的时候判充电桩能充的电总量赋给每一个机器后最后还剩。

代码注意:while(r-l>1e-10)虽然保证精度,但是TLE31.放小了也tle。干脆改成固定循环一百次。上界取1e19近乎LL最大,不然wa31

#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout<<#a<<"="<c[i].b){
			ans-=1.0*c[i].a*x-1.0*c[i].b;
		}		
	}
	if(ans>=0) return true;
	else return false;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>n>>p;
  LL sum1=0;LL sum2=0;
  for(LL i=1;i<=n;i++){
  	cin>>c[i].a>>c[i].b;
  	sum1+=c[i].a;sum2+=c[i].b;
  }
  	double l=0.0;double r=1e19;
//  while(r-l>1e-6){
	for(LL i=0;i<100;i++){
  	    double mid=(1.0*l+1.0*r)/2;
		if(check(mid)) l=mid;
		else r=mid;	
	}
	if(l>=1e18) printf("-1\n");
	else printf("%.10f\n",l);
return 0;
}

 

你可能感兴趣的:(二分题型刷题总结)