洛谷评级就是辣鸡
原题地址:https://vjudge.z180.cn/problem/CodeForces-782B
题面:
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.
Input
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.
Output
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.
Examples
Input
3
7 1 3
1 2 1
Output
2.000000000000
Input
4
5 10 3 2
2 3 2 4
Output
1.400000000000
大意:
在一条直线上站在 n 个朋友,第 i 个朋友的位置为 Xi ,并且可以以不超过Vi每秒的任意速度向左或者向右移动。请计算最少需要多少秒。
显然要二分答案,用check()判断当前的时间是不是可行时间,如果可行,那么说明答案小于等于可行时间,更新右边界。反之更新左边界。
所以问题就简化成了如何判断二分点是否可行,也就是check()的写法,由于每个人可以向左或向右走,所以每一个时间都对应一个该人的移动范围,只要这个范围有一个共同的交集,那么说明该时间是可行时间。
所以问题又简化成了如何确定每个人的移动范围是否存在交集。
画一下第一个样例
可以看到7号点和1,3号点在5处刚好有一个交集,由此启发,只要右边界最靠左的点的坐标小于左边界最靠右的点,那么就是有交集。可以画下面一个极端 数据解释:
code
#include<bits/stdc++.h>
using namespace std;
const double eps=0.000001;//题面中的精度要求
int n;
struct node
{
double v,x;
}a[60010];
bool cmp(node xx,node y)
{
return xx.x<y.x;
}//忽略掉我开始的憨憨操作,留着它是要提醒我们二分答案一般不需要排序
bool check(double t)//判断每个点是否存在一个交点
{
double ll=2147483647,rr=-2147483647;//学会用const int inf=0x3f3f3f3f3f;好一点
for(int i=1;i<=n;i++)
{
if(a[i].x+t*a[i].v<ll)
ll=a[i].x+t*a[i].v;
if(a[i].x-t*a[i].v>rr)
rr=a[i].x-t*a[i].v;
}
return ll>=rr;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%lf",&a[i].x);
for(int i=1;i<=n;i++)
scanf("%lf",&a[i].v);
//sort(a+1,a+n+1,cmp);忽略掉我开始的憨憨操作
double l=0,r=2147483647;
while(r-l>eps) //平平无奇的二分
{
double mid=(l+r)/2;
if(check(mid))
r=mid;
else
l=mid;
}
printf("%.12f",l);
return 0;
}