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 (1≤n≤10001≤n≤1000), the number of sections in Sheila’s journey, and tt (1≤t≤1061≤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 (1≤di≤10001≤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 10−610−6.
Sample Input 1 | Sample 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 |
#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);
}