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 v, her true speed is v+c, where c is an unknown constant (possibly negative).

Sheila made a careful record of a recent journey and wants to use this to compute c. The journey consisted of n segments. In the ith segment she traveled a distance of di and the speedometer read vi for the entire segment. This whole journey took time t. Help Sheila by computing c.

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 (1≤n≤1000), the number of sections in Sheila’s journey, and t (1≤t≤106), the total time. This is followed by n lines, each describing one segment of Sheila’s journey. The ith of these lines contains two integers di (1≤di≤1000) and v (|vi|≤1000), the distance and speedometer reading for the ith segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.

Output

Display the constant c in miles per hour. Your answer should have an absolute or relative error of less than 10−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

题意:

该旅程分为n个部分,每个部分的距离为d[i],速度表所读取的速度为v[i] (v>0),但真实速度为v[i]+c。已知旅程总时间为t,在总误差不超过1e-6的条件下,求c。

思路:

对于方程SUM(d[i]/s[i]+c),利用二分求解的方法求c。

#include<bits/stdc++.h>
#define MAX 1050
using namespace std;
int d[MAX],v[MAX],n;
double t;
int ok(double c)
{
double T=;
for(int i=;i<n;i++)
{
if(c+v[i]<=)return ; //c偏小
else T+=1.0*d[i]/(v[i]*1.0+c);
}
if(T>t)return ;
else return ; //c偏大
}
int main()
{
int i;
while(scanf("%d%lf",&n,&t)!=EOF)
{
for(i=;i<n;i++)
scanf("%d%d",&d[i],&v[i]);
double low=-1e9,high=1e9;
while(high-low>1e-)
{
double mid=(high+low)*0.5;
if(ok(mid))
high=mid;
else low=mid;
}
printf("%.9f\n",(high+low)*0.5);
}
return ;
}

最新文章

  1. IOS开发基础知识--碎片45
  2. 正则表达式测试器 beta_
  3. nodemailer实现node发送邮件
  4. 使用 SVG 制作单选和多选框动画【附源码】
  5. ubuntu下取代ping的好工具tcpping
  6. Linux服务器时间同步方法
  7. 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a
  8. RHEL6.4 KVM 桥接上网的设置
  9. codeforces #313 div1 E
  10. .NET 4.0中的泛型协变和逆变
  11. SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
  12. ansible学习之路
  13. linux设置静态IP和DNS以及改网卡名
  14. [用CDQ分治解决区间加&amp;区间求和]【习作】
  15. JS直接调用C#后台方法(ajax调用)
  16. IdentityServer4 密码模式认证
  17. styled-components的基本使用
  18. 【代码审计】大米CMS_V5.5.3 代码执行漏洞分析
  19. ASP.Net 获取Form表单值
  20. centos7:mysql-5.7.23安装(二进制安装)

热门文章

  1. C# HashTable 使用用法详解
  2. drupal 基础理论
  3. sqlalchemy &amp; python &amp; datatables &amp; javascript 中文拼音排序
  4. 【NLP_Stanford课堂】正则表达式
  5. 【Leetcode】【Easy】ZigZag Conversion
  6. WAKE-WIN10-SOFT-GITHUB
  7. apache poi根据模板导出excel
  8. May 22nd 2017 Week 21st Monday
  9. 微信小程序国际化
  10. BZOJ2836:[SHOI2012]魔法树(树链剖分)