这个是国外原本的oj(在国内可能会很卡顿)点击打开链接
这个是vj的网址 有时候可能会提交失败点击打开链接
题意:
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 (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 10 5 3 2 2 3 6 3 1 | -0.508653377 |
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<iostream>
#include<set>
#include<map>
#include<string.h>
using namespace std;
int k[1050],ll[1050];
int main()
{
int n,t;
double l,r,sum,mid;
scanf("%d%d",&n,&t);
for(int i=0;i<n;i++)
scanf("%d%d",&k[i],&ll[i]);
l=-0x7f7f7f7f,r=0x7f7f7f7f;
while(r-l>0.0000001)
{
mid=(r+l)/2;
sum=0;
for(int i=0;i<n;i++)
{
if(mid+ll[i]<0)
{
sum=t+1;
break;
}
sum+=k[i]/(ll[i]+mid);
}
//printf("%lf\n",mid);
if(sum<t)
r=mid;
else
l=mid;
}
printf("%lf\n",l);
return 0;
}