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 ;
}

【2017 World Final E】Need For Speed(二分)的更多相关文章

  1. 8VC Venture Cup 2016 - Final Round D. Preorder Test 二分 树形dp

    Preorder Test 题目连接: http://www.codeforces.com/contest/627/problem/D Description For his computer sci ...

  2. 【bzoj4952】[Wf2017]Need for Speed 二分

    题目描述 已知$\sum\limits_{i=1}^n\frac{d_i}{s_i+c}=t$,求$c$ $(d_i>0,s_i+c>0)$ 输入 第一行包含两个整数n(1≤n≤1000) ...

  3. 2017 world final

    E 解题关键:二分时注意C函数的单调性. #include<bits/stdc++.h> #define eps 1e-8 #define INF 0x3f3f3f3f using nam ...

  4. Intel Code Challenge Final Round D. Dense Subsequence 二分思想

    D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. World Finals 2017 (水题题解)

    看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...

  6. 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器

    基于HTML5和CSS的焦点图特效,梅花图案的背景很有中国特色,而且还会动哦,效果超炫,推荐下载! 演示图 html代码 <!DOCTYPE html PUBLIC "-//W3C// ...

  7. dedecms织梦建站总结

    说好要每月坚持写博客的,差一点就背弃自己的诺言了. 这一个月,除了修改magento站点和学习android外,一心都投在了为一家建筑公司做网站上去了,使用的是dedecms,我主要做的是前端开发,着 ...

  8. dart基础语法

    .关于 runApp() 上面的实例代码中使用了 runApp() 方法,runApp 方法接收的指定参数类型为 Widget,即: runApp(Widget).在 Flutter 的组件树(wid ...

  9. Spark记录-Scala类和对象

    本章将介绍如何在Scala编程中使用类和对象.类是对象的蓝图(或叫模板).定义一个类后,可以使用关键字new来创建一个类的对象. 通过对象可以使用定义的类的所有功能. 下面的图通过一个包含成员变量(n ...

随机推荐

  1. 互联网轻量级框架SSM-查缺补漏第一天

    简言:工欲其事必先利其器,作为一个大四的准毕业生,在实习期准备抽空补一下基础.SSM框架作为互联网的主流框架,在会使用的基础上还要了解其原理,我觉得会对未来的职场会有帮助的.我特意的买了一本<J ...

  2. 使用jquery去掉时光轴头尾部的线条

    一.前言:以前做类似时光轴的结构,几乎都是一条灰色线飞流直下,没有尽头.今天这个线条是从第一个圆点到最后一个圆点,那么问题来了,内容的高度还不是固定的,线条的长度怎么确定?怎么就能刚刚好从第一个点到最 ...

  3. pId的数据结构转children 数据结构(JS);

    在工作中经常遇到需要把带有pId的的list数据转换为children格式的树形结构,一直都没有找到太好的工具函数.偶然间看到了这个函数,研究了下,感觉这个函数很强大,所以记录下来,作为备用,同时也贴 ...

  4. axios 发 post 请求,后端接收不到参数的解决方案

    问题场景 场景很简单,就是一个正常 axios post 请求: axios({ headers: { 'deviceCode': 'A95ZEF1-47B5-AC90BF3' }, method: ...

  5. JS的封装(JS插件的封装)

    JS中类的概念类,实际上就是一个function,同时也是这个类的构造方法,new创建该类的实例,new出的对象有属性有方法.方法也是一种特殊的对象. 类的方法在构造方法中初始化实例的方法(就是在构造 ...

  6. window.frames在不同浏览器中的用法

    document.frames 等同于 window.frames,用来取得当前页面内 window 对象的集合. 不支持Firefox,其他浏览器(chrome.opera.IE.360)均支持. ...

  7. VS如何自动创建函数头 函数说明

    这样创建,这种方法在VS2008和VS2013下都可以. 这是效果 如何自定义格式呢? 第一步点击这个按钮 这个工具条需要安装VAssistX,如何安装这里不细说了. 第二步,点击这个按钮 第三步 点 ...

  8. Android 屏幕录制

    自己实现了Android的屏幕录制App. 用了MediaProjection类来作为源,MediaRecoder来捕捉,编码转换为本地视频. 效果图: 主要是这段代码开始录像: startActiv ...

  9. SolidWorks二次开发的研究

    三维机械设计软件SolidWorks是一套基于Windows的CAD/CAE/CAM/PDM桌面集成系统,是由美国SolidWorks公司在总结和继承大型机械CAD软件的基础上,在Windows环境下 ...

  10. solidity语言3

    #函数类型(function type) function (<parameter types>) {internal|external(public)} [pure|constant|v ...