Need for Speed

 

Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart. Recently, the needle on the speedometer fell off. She glued it back on, but she might have placed it at the wrong angle. Thus, when the speedometer reads ss, her true speed is s+cs+c, where cc is an unknown constant (possibly negative).

Sheila made a careful record of a recent journey and wants to use this to compute cc. The journey consisted of nn segments. In the ithith segment she traveled a distance of didi and the speedometer read sisi for the entire segment. This whole journey took time tt. Help Sheila by computing cc.

Note that while Sheila’s speedometer might have negative readings, her true speed was greater than zero for each segment of the journey.

Input

The first line of input contains two integers nn (1≤n≤10001≤n≤1000), the number of sections in Sheila’s journey, and tt(1≤t≤1061≤t≤106), the total time. This is followed by nn lines, each describing one segment of Sheila’s journey. The ithith of these lines contains two integers didi (1≤di≤10001≤di≤1000) and sisi (|si|≤1000|si|≤1000), the distance and speedometer reading for the ithith segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.

Output

Display the constant cc in miles per hour. Your answer should have an absolute or relative error of less than 10−610−6.

Sample Input 1 Sample Output 1
3 5
4 -1
4 0
10 3
3.000000000
Sample Input 2 Sample Output 2
4 10
5 3
2 2
3 6
3 1
-0.508653377

wa了好久了,搞不懂二分范围,以为-1000到1e6+1000就够了,我一开大范围反而得不到答案

通过读题列出通项公式,很容易想到二分操作

#include<bits/stdc++.h>
#define eps 1e-8
using namespace std;
typedef long long ll;
double a[],b[];
int n,m;
double solve(){
double l=-<<,r=<<,mid;
for(int i=;i<n;i++)
l=max(l,-b[i]);
for(int i=;i<;i++){
mid=(l+r)/;
double sum=0.0;
for(int j=;j<n;j++){
sum+=a[j]/(mid+b[j]);
}
if(sum-m>) l=mid;
else r=mid;
}
return r;
}
int main(){
cin>>n>>m;
for(int i=;i<n;i++)
cin>>a[i]>>b[i];
double ans=solve();
printf("%.8lf\n",ans);
}

最新文章

  1. C、C++、Java、go的语法区别
  2. 使用Jayrock开源组件开发基于JSON-RPC协议的接口
  3. js控制控件不可编辑
  4. Sublime怎样新建HTML文档
  5. 嵌入式 uboot引导kernel,kernel引导fs
  6. Oracle配置
  7. awk将普通文本转换成json文件
  8. kvc模式详解
  9. Jmeter使用代理服务器录制脚本
  10. java学习——java按值传递和按址传递
  11. Linux系统调用的实现机制分析
  12. 爬虫豆瓣top250项目-开发文档
  13. vue传参二
  14. 【BZOJ3142】[HNOI2013]数列(组合计数)
  15. springmvc后台生成验证码
  16. apply的妙用
  17. LinuxShell脚本编程基础1-vi编辑器的使用
  18. 关于sql优化整理一下
  19. 【kubernetes】ubuntu14.04 64位 搭建kubernetes过程
  20. 如何用python语言撸出图表系统

热门文章

  1. 使用JDBC进行简单的增删改查
  2. 在Window上用cmd创建.htaccess文件
  3. 浅析ES6中的iterator
  4. SQL Server 2008添加字段成功,但提示列名无效
  5. 在页面实现qq跳转链接
  6. Android 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
  7. android Random的使用
  8. phantomas参数选项
  9. Openjudge 1.13-23:区间内的真素数
  10. java在线聊天项目1.0版 异常处理——开启多个客户端,关闭一个客户端后,在其他客户端中再发出信息会出现异常的处理