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 ithithof 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

题意: 你有1个坏的仪表盘,他显示的速度和正确的速度相差 c,现在给你个数n和行驶总时间t ,接下来n行 ,每行1个行驶距离 d和显示速度s ,现在要求偏差值c 。、

解:比如第1组样例 总时间 5 = 4/(c-1)  + 4/(c+0)  +10/(3+c)  ,因为时间 =路程/真实速度 ;

那么我们可以二分枚举 c 看是否满足要求 ,首先每个 c+s 必须大于0 ,否则说明 c太小了 ,

其次 将得到的总时间 和 t进行比较 ,若小于 t 说明c太大 ,若大于t ,说明c太小

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const double eps=1e-9;
int n;
double t;
double d[1100],s[1100];
int aa(double c)
{
    double kk=0;
    for(int i=1;i<=n;i++)
    {
        if(c+s[i]<=0)
            return 0;
        kk+=d[i]/(c+s[i]);
    }
    if(kk>t) return 0;
    else  return 1;
}
int main()
{
    scanf("%d%lf",&n,&t);
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&d[i],&s[i]);
     double hign=1e8+1;
     double low=-1e8;
     double mid;
     while(hign-low>eps)//因为可能存在误差
     {
         mid=(hign+low)/2;
         if(aa(mid)) hign=mid;
         else low=mid;
     }
     printf("%.6lf\n",hign);
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值