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 ithithsegment 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 (1n10001≤n≤1000), the number of sections in Sheila’s journey, and tt (1t1061≤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(1di10001≤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 10610−6.

Sample Input 1Sample Output 1
3 5
4 -1
4 0
10 3
3.000000000
Sample Input 2Sample Output 2
4 10
5 3
2 2
3 6
3 1
-0.508653377
#include <iostream>
#include <stdio.h>
using namespace std;
int s[1001],v[1001],n;
double t;
int judge(double c)
{
    int i;
    double sum=0;
    for(i=0;i<=n-1;i++){
        if(v[i]+c<=0)return -1;//这里做了个优化,而且避免除零
        else sum+=s[i]*1.0/(v[i]+c);
    }
    if(sum<t){
        //ri=c;
        return 1;
    }else //le=c;
    return -1;
}
int main()
{
    int i;
    double ri=1e9,le=-1e9,mid=(ri+le)/2;

    scanf("%d%lf",&n,&t);
    for(i=0;i<=n-1;i++)scanf("%d%d",&s[i],&v[i]);
    while(ri-le>=1e-9){//这里虽然要求1e-6,但是直接写1e-6算出来不精确

        if(judge(mid)>0){
            ri=mid;
            mid=(ri+le)/2;
        }
        else{
            le=mid;
            mid=(ri+le)/2;
        }
    }
    printf("%.9lf\n",mid);
    return 0;
}

这道题折腾了好久啊quq,写出来也不太难,但是想的时候用了好长好长时间,为什么啊啊啊啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值