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 , her true speed is , where is an unknown constant (possibly negative).
Sheila made a careful record of a recent journey and wants to use this to compute . The journey consisted of segments. In the segment she traveled a distance of and the speedometer read for the entire segment. This whole journey took time . Help Sheila by computing .
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 (), the number of sections in Sheila’s journey, and (), the total time. This is followed by lines, each describing one segment of Sheila’s journey. The of these lines contains two integers () and (), the distance and speedometer reading for the segment of the journey. Time is specified in hours, distance in miles, and speed in miles per hour.
Output
Display the constant in miles per hour. Your answer should have an absolute or relative error of less than .
Sample Input 1 | Sample Output 1 |
---|---|
3 5 4 -1 4 0 10 3 | 3.000000000 |
Sample Input 2 | Sample Output 2 |
---|---|
4 10 5 3 2 2 3 6 3 1 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int d[1100],s[1100];
double c;
int n,t;
int solve(double c)
{
double te=0;
for(int i=1;i<=n;i++)
{
if(c+s[i]<=0) return -1;
else
{
te+=1.0*d[i]/(s[i]+c);
}
}
if(te<t)
return 1;
else
return -1;
}
int main()
{
while(scanf("%d%d",&n,&t)!=EOF)
{
double l=-1e9,r=1e9,mid=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&d[i],&s[i]);
}
while(r-l>=1e-6)
{
if(solve(mid)==1)
{
r=mid;
mid=0.5*(r+l);
}
else
{
l=mid;
mid=0.5*(r+l);
}
}
printf("%.9lf\n",mid);
}
return 0;
}