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

Sheila made a careful record of a recent journey and wants to use this to compute  c c. The journey consisted of  n n segments. In the  ith ith segment she traveled a distance of  di di and the speedometer read  si si for the entire segment. This whole journey took time  t t. Help Sheila by computing  c 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  n n ( 1n1000 1≤n≤1000), the number of sections in Sheila’s journey, and  t t ( 1t106 1≤t≤106), the total time. This is followed by  n n lines, each describing one segment of Sheila’s journey. The  ith ith of these lines contains two integers  di di ( 1di1000 1≤di≤1000) and  si si ( |si|1000 |si|≤1000), the distance and speedometer reading for the  ith ith segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.

Output

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


直接二分就好了,注意每一组加完之后的速度都不能是负数,如果是出现负数就可以直接往右找了

ac代

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
double a[1003][2];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
       for(int i=0;i<n;i++)
       {
           scanf("%lf%lf",&a[i][0],&a[i][1]);
       }
       double l=-10000000,r=10000000;
       double mid;
       while(r-l>0.0000001)
       {
         mid=(l+r)/2;
         double sum=0;
         for(int i=0;i<n;i++)
         {
             if(a[i][1]+mid<0)
             {
                 sum=100000000;
                 break;
             }
            sum+=(a[i][0]/(a[i][1]+mid));
         }
         if(sum<m)
         r=mid;
         else
         l=mid;
       }
       printf("%.7lf\n",l);
    }
    return 0;
}

  
  
码:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值