Kattis - speed

本文介绍了一种通过已知行程和时间来计算车辆速度计误差的方法。利用二分查找算法确定速度计显示值与实际速度之间的常数误差。文章提供了一个具体的实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ithith segment 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 2

Sample Output 2

4 105 32 23 63 1

-0.508653377


题意:
       给出n段车速,m为n段车速的正确平均速度,在n段车速中存在误差,正确为s+c,求c。

思路:
       二分查找,double型小数的二分。需要控制精度。还有一个坑点在于每一段的速度只能是正数,也就是说s+c>0,这里需要特判。

#include<stdio.h>
const double ppx=0.0000000001;
int n;
double m,s[1010],t[1010];
int main()
{
    scanf("%d%lf",&n,&m);
    double sum=0,pans;
    for(int i=0; i<n; i++)
    {
        scanf("%lf%lf",&s[i],&t[i]);
        sum+=s[i];
    }
    pans=sum/m;
    int flag;
    double l=-1e8,r=1e8,pan,ans;
    while(l<=r)
    {
        flag=0;
        pan=(l+r)/2;
        ans=0;
        for(int i=0; i<n; i++)
        {
            ans=ans+s[i]/(t[i]+pan);
            if(t[i]+pan<=0)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            l=pan+ppx;
            continue;
        }
        else if(ans-m>ppx)
            l=pan+ppx;
        else
            r=pan-ppx;
    }printf("%.9lf\n",pan);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值