二分

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


题意:简化的认为是一个方程的求解:
对SUM(di/(si+c))=t,利用二分求解的方法

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值