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 point xi meters and can move with any speed no greater than vi 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 the n friends at some point on the road. Note that the point they meet at doesn't need to have integer coordinate.
The first line contains single integer n (2 ≤ n ≤ 60 000) — the number of friends.
The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) — the current coordinates of the friends, in meters.
The third line contains n integers v1, v2, ..., vn (1 ≤ vi ≤ 109) — the maximum speeds of the friends, in meters per second.
Print the minimum time (in seconds) needed for all the n friends to meet at some point on the road.
Your answer will be considered correct, if its absolute or relative error isn't greater than 10 - 6. Formally, let your answer be a, while jury's answer be b. Your answer will be considered correct if holds.
3 7 1 3 1 2 1
2.000000000000
4 5 10 3 2 2 3 2 4
1.400000000000
In the first sample, all friends can gather at the point 5 within 2 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.
Source
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
My Solution
题意:n个人每个人在xi位置且运行速度为vi,问他们相聚在一点的最短时间。
三分
打那次cf 的时候,三分还没有学,没办法。
这里直接对[minx, maxx]的范围内进行三分即可,
然后eps最开始的时候取了1e-12,后来TLE了,确实好像数据溢出了,double大概只能存十五六位有效数字。
所以改成1e-6,就过来,这里给出的 一是为了提醒eps的取值,
然后可以算一下,用1e-6时,计算出的答案误差确实最大为1e-6,刚好可以满足<=1e-6的误差要求,所以可以放心使用。
复杂度 略大于 O(nlogn)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long long LL;
const int MAXN = 1e6 + 8;
const double EPS = 1e-6;
int n, x[MAXN], v[MAXN];
inline double getsum(double mid)
{
double res = 0;
for(int i = 0; i < n; i++){
res = max(res, fabs(mid - x[i]) / v[i]);
}
return res;
}
int main()
{
#ifdef LOCAL
freopen("b.txt", "r", stdin);
//freopen("b.out", "w", stdout);
int T = 4;
while(T--){
#endif // LOCAL
ios::sync_with_stdio(false); cin.tie(0);
int i;
double l = 1e9 + 8, r = 0, ll, rr, ans = 0, ans1, ans2;
cin >> n;
for(i = 0; i < n; i++){
cin >> x[i];
l = min(l, (double)x[i]);
r = max(r, (double)x[i]);
}
for(i = 0; i < n; i++) cin >> v[i];
ans = min(getsum(l), getsum(r)); //答案一般不会取到边界,但有时候可能可以
while(l + EPS < r){
ll = l + (r - l) / 3;
rr = r - (r - l) / 3;
ans1 = getsum(ll);
ans2 = getsum(rr);
if(ans1 > ans2){
l = ll;
}
else r = rr;
ans = min(ans, min(ans1, ans2));
}
cout << fixed << setprecision(12) << ans << endl;
#ifdef LOCAL
cout << endl;
}
#endif // LOCAL
return 0;
}
Thank you!
------from ProLights