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

Sheila made a careful record of a recent journey and wants to use this to compute c

. The journey consisted of n segments. In the ith segment she traveled a distance of di and the speedometer read si for the entire segment. This whole journey took time t . Help Sheila by computing 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

( 1n1000 ), the number of sections in Sheila’s journey, and t ( 1t106 ), the total time. This is followed by n lines, each describing one segment of Sheila’s journey. The ith of these lines contains two integers di ( 1di1000 ) and si ( |si|1000 ), the distance and speedometer reading for the ith

segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.

Output

Display the constant c

in miles per hour. Your answer should have an absolute or relative error of less than 106

.

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
题意:给你n组数据代表每组的路程和速度,但这些速度是错误的,与正确的速度相差了c(可能为正可能为负),给你总时间,要你求出c,精度控制住1e-6.

二分找答案

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#define inf 0x3f3f3f3f
typedef long long LL;
const int mod=1e9+7;
const double eps=1e-6;
const double pi=acos(-1);
const int N=1e3+1;
int n;
double t,d[N],s[N];
double f(double x)
{
    double sum=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]+x<0)
        {
            sum=inf;
            break;
        }
        sum+=d[i]/(s[i]+x);
    }
    return sum;
}
void solve()
{
    double l=-inf,r=inf,mid;
    while(r-l>eps)
    {
        mid=(r+l)/2;
        if(f(mid)<t) r=mid;
        else l=mid;
    }
    printf("%.6f\n",mid);
}
int main()
{
    printf("%f %f\n",inf,infi);
    printf("%d %d",inf,infi);
    scanf("%d%lf",&n,&t);
    for(int i=0;i<n;i++)
        scanf("%lf%lf",&d[i],&s[i]);
    solve();
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值