题目描述

已知$\sum\limits_{i=1}^n\frac{d_i}{s_i+c}=t$,求$c$ $(d_i>0,s_i+c>0)$

输入

第一行包含两个整数n(1≤n≤1000)和t(1≤t≤10^6),分别表示Sheila的行程段数和总时间。
接下来n行,每行描述了Sheila的一段行程。
第i行包含两个整数di(1≤di≤1000)和si(|si|≤1000),分别表示第 i 段行程的距离和表盘读数。
时间单位是小时,距离单位是英里,速度单位是英里每小时。

输出

输出常数c,其单位是英里每小时。你的答案绝对或相对误差应该小于10^-6。

样例输入

3 5
4 -1
4 0
10 3

样例输出

3.000000000


题解

二分

由于给出的函数是单调的,因此可以直接二分c的取值并判断即可。

注意精度问题,最好使用long double并进行固定100次二分迭代。

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long double ld;
ld d[1010] , s[1010];
int main()
{
int n , i , cnt = 100;
ld t , l = 1e18 , r = 1e18 , mid , ret;
scanf("%d%Lf" , &n , &t);
for(i = 1 ; i <= n ; i ++ ) scanf("%Lf%Lf" , &d[i] , &s[i]) , l = min(l , s[i]);
l = -l;
while(cnt -- )
{
mid = (l + r) / 2 , ret = 0;
for(i = 1 ; i <= n ; i ++ ) ret += d[i] / (s[i] + mid);
if(ret < t) r = mid;
else l = mid;
}
printf("%.9Lf\n" , l);
return 0;
}

【bzoj4952】[Wf2017]Need for Speed 二分的更多相关文章

  1. 【2017 World Final E】Need For Speed(二分)

    Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart. ...

  2. 【BZOJ4952】lydsy七月月赛 E 二分答案

    [BZOJ4952]lydsy七月月赛 E 题面 题解:傻题...二分答案即可,精度有坑. #include <cstdio> #include <cstring> #incl ...

  3. CF2.C(二分贪心)

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Codeforces Round #379 (Div. 2) A B C D 水 二分 模拟

    A. Anton and Danik time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

  6. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  7. Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2)C. Road to Cinema 二分

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 二分

    C. Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input ...

  9. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

随机推荐

  1. NestedScrollView和RecyclerView使用,并设置间距

    NestedScrollView和RecyclerView使用,并设置间距: 效果图如下: 1.NestedScrollView 和RecyclerView嵌套问题(类似ScrollView 和lis ...

  2. 08_1_IO

    08_1_IO 1. 输入/输出流的分类 java.io包中定义了多个流类型(类或抽象类)来实现输入/输出功能:可以从不同的角度对其进行分类: 按数据流的方向不同可以分给输入流和输出流. 按处理数据单 ...

  3. OCCI的迭代修改

    传统的在执行多行DML(INSERT.UPDATE.DELETE)时,我们是多次调用executeUpdate():注意!当我们调用一次此函数时,则执行一次网络往返,当数据量大时则效率非常低.不过 O ...

  4. httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

    httpd配置内容 httpd2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf 服务脚本: /etc/rc.d/init.d/ ...

  5. ECSHOP快递物流单号查询插件

    本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...

  6. GMT 时间格式转换到 TDateTime (Delphi)

    //GMT 时间格式转换到 TDateTime //忽略时区 function GMT2DateTime(const pSour:PAnsiChar):TDateTime; function GetM ...

  7. 554. Brick Wall

    class Solution { public: int leastBricks(vector<vector<int>>& wall) { unordered_map& ...

  8. python基础之生成器表达式形式、面向过程编程、内置函数部分

    生成器表达式形式 直接上代码 1 # yield的表达式形式 2 def foo(): 3 print('starting') 4 while True: 5 x=yield #默认返回为空,实际上为 ...

  9. Idea搭建spring framework源码环境

    spring的源码目前放在github上,https://github.com/spring-projects/spring-framework 一.安装Git 二.安装Gradle gradle为解 ...

  10. Maven学习 (五) Elipse中发布一个Maven项目到Tomcat

    对于maven初学者的我,经常遇到一个问题就是,maven项目创建成功后,本来已经添加了jar的依赖,但是发布到Tomcat中就是没有jar包存在, 启动Tomcat总是报没有找到jar包,可项目结构 ...