1.题目描述:
一条马路上有若干个人,每个人位于xi上,他们奔跑的速度为vi,问你最短时间使得他们都汇集在某点(不一定是整数点)
3.解题思路:
因为不一定是整数点,所以没必要枚举坐标,直接二分答案就行,这题好像允许和答案有一定的误差范围。对于每次二分的答案judge一下所有的人向左走的最短位置和向右走的最短位置,如果maxl > minr则肯定无解,如果有解的话再在更短的时间内二分。
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 60066
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
int x, v;
} p[maxn];
bool judge(double t, int n)
{
double l = 0, r = 1e10;
for (int i = 0; i < n; i++)
{
l = max(l, p[i].x - p[i].v * t);
r = min(r, p[i].x + p[i].v * t);
}
if (l > r)
return 0;
return 1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
scanf("%d", &p[i].x);
for (int i = 0; i < n; i++)
scanf("%d", &p[i].v);
double ans = 0, l = 0, r = 1e9, mid;
while (r - l > eps)
{
mid = (l + r) / 2;
if (judge(mid, n)) //如果可以到达则缩短时间 {
{
r = mid - eps;
ans = mid;
}
else
l = mid + eps;
}
printf("%.10f\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}