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.
/*
二分。
题意很简单 给你个n个点 这些点都在一条直线上 我将它们视为都在x轴上
然后给出n个点的坐标和最大移动的速度,移动的方向没有规定 可左可右
求出这儿n个点相遇的最短时间 感觉是初高中追击相遇问题的升级版 噜噜噜-0-
这题有两种方法 目前我知道的:
①二分距离 ②二分速度
*/
/*
解法一:二分距离
因为我是觉得相遇的那个距离 肯定是在最左边的点和最右边的那个点这两个点的中间。
即[L,R]。 L为最左边那个点的坐标 R为最右边那个点的坐标 然后开始二分距离
那些点移动的速度肯定是取最大值的 因为你要保证相遇的时间最短
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
using namespace std;
const int maxn = 60000+100;
const double eps = 1e-8;
struct node
{double x,v;
}p[maxn];
int n;
int main(void)
{
scanf("%d",&n);
double l=1e9,r=0;
for(int i=1;i<=n;i++)
{
scanf("%lf",&p[i].x);
l=min(l,p[i].x);
r=max(r,p[i].x);
}
for(int i=1;i<=n;i++)
scanf("%lf",&p[i].v);
int mark;
double ans=0,time;
while(r-l>eps)
{
ans=0;
double mid=(l+r)/2;
for(int i=1;i<=n;i++)
{
time=fabs(mid-p[i].x)/p[i].v;
if(ans<time)
{
ans=time;
mark=i;
}
}
if(p[mark].x>mid) l=mid;
else r=mid;
}
printf("%.6f\n",ans);
return 0;
}
/*
解法二:二分速度 ····by住辉大大
先扫一遍找个最大时间,然后二分 【0,MaxTime】
判断满不满足的时候 把每个点用速度和二分的那个时间去拆成一个区间
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 60005;
const double EPS = 1e-7;
struct Node{
int pos,v;
}a[maxn];
struct Node1{
double val;
int flag;
}qu[2*maxn];
int n;
bool cmp(const Node&a,const Node&b){
return a.pos<b.pos;
}
bool cmp1(const Node1&a,const Node1&b){
if(a.val==b.val)return a.flag<b.flag;
return a.val<b.val;
}
bool calc(double Time)
{
double ans=Time;
for(int i=0;i<n;i++){
qu[i].flag=0;
qu[i+n].flag=1;
qu[i].val=a[i].pos-a[i].v*Time;
qu[i+n].val=a[i].pos+a[i].v*Time;
}
sort(qu,qu+2*n,cmp1);
int sign=0;
for(int i=0;i<n;i++){
if(qu[i].flag==0){
sign++;
if(sign==n){
return true;
}
}
else sign--;
}
return false;
}
double solve(double L, double R)
{
while(R-L>EPS){
double M=(L+R)*1.0/2.0;
if(calc(M))R=M;
else L=M;
}
return L;
}
int main()
{
while(~scanf("%d",&n)){
for(int i=0;i<n;i++){
scanf("%d",&a[i].pos);
}
for(int i=0;i<n;i++){
scanf("%d",&a[i].v);
}
sort(a,a+n,cmp);
double TimeMax=0;
for(int i=0;i<n;i++){
double a1=(a[i].pos-a[0].pos)*1.0/a[i].v;
double a2=(a[i].pos-a[n-1].pos)*1.0/a[i].v;
TimeMax=max(TimeMax,max(a1,a2));
}
double ans=solve(0.0,TimeMax);
printf("%.6f\n",ans);
}
return 0;
}