首页 > 学院 > 开发设计 > 正文

Codeforces Round #403 (Div. 2)B. The Meeting Place Cannot Be Changed

2019-11-06 06:48:44
字体:
来源:转载
供稿:网友
B. The Meeting Place Cannot Be Changedtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction.

At some points on the road there are n friends, and i-th of them is standing at the pointxi meters and can move with any speed no greater thanvi meters per second in any of the two directions along the road: south or north.

You are to compute the minimum time needed to gather all then friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.

Input

The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.

The second line contains n integersx1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.

The third line contains n integersv1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.

Output

PRint the minimum time (in seconds) needed for all then friends to meet at some point on the road.

Your answer will be considered correct, if its absolute or relative error isn't greater than10 - 6. Formally, let your answer bea, while jury's answer be b. Your answer will be considered correct if holds.

ExamplesInput
37 1 31 2 1Output
2.000000000000Input
45 10 3 22 3 2 4Output
1.400000000000Note

In the first sample, all friends can gather at the point5 within2 seconds. In order to achieve this, the first friend should go south all the time at his maximum speed, while the second and the third friends should go north at their maximum speeds.

题意分析:在一条直线上有n个朋友,每个朋友的位置为xi,每个朋友最大可以以速度vi移动。问在最快的时间内所有朋友都移动到一个点。输出最短的时间。集合到的位置可以不必为整数。

这里可以使用二分法,二分时间,在t时间内,算出每个人移动的最大范围,有个上限pmax,和下限pmin,只要上限的最小值大于等于下限的最大值就满足所有人移动到同一点。

刚开始二分的条件设置太小了,high - low > 0.000000001.导致第三个样例超时。如果设置太大又会造成答案错误。

参考代码:

#include <iostream>#include <string.h>#include <algorithm>#include <set>#include <math.h>using namespace std;const int N = 100000;double x[N], v[N], t[N];double pmax[N], pmin[N];int n;bool f(double t){	for (int i = 0; i < n; i++)	{		pmax[i] = x[i] + t*v[i];		if (pmax[i] > 1000000000)			pmax[i] = 1000000000;		pmin[i] = x[i] - t*v[i];		if (pmin[i] < 0)			pmin[i] = 0;	}	sort(pmin, pmin + n);	sort(pmax, pmax + n);	if (pmax[0] >= pmin[n - 1])		return true;	return false;}double bs(){	double low = 0, high = 10000000000;	double mid = low + (high - low) / 2.0;	while (high - low > 0.0000001)	{		if (f(mid))			high = mid-0.00000001;		else low = mid+0.00000001;		mid = low + (high - low) / 2.0;	}	return mid;}int main(){	scanf("%d", &n);	for (int i = 0; i < n; i++)		scanf("%lf", &x[i]);	for (int i = 0; i < n; i++)		scanf("%lf", &v[i]);		printf("%f/n", bs());	//system("pause");	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表