782B. The Meeting Place Cannot Be Changed 二分 水

时间:2021-04-27 12:41:35

Link

题意:给出$n$个坐标$x_i$,$n$个速度$v_i$问使他们相遇的最短时间是多少。

思路:首先可肯定最终相遇位置必定在区间$[0,max(x_i)]$中,二分最终位置,判断左右部分各自所花时间,缩小范围即可。

/** @Date    : 2017-05-09 22:07:43
* @FileName: 782B.cpp
* @Platform: Windows
* @Author : Lweleth (SoundEarlf@gmail.com)
* @Link : https://github.com/Lweleth
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-6; double p[60010];
double v[60010];
double tim;
int n;
int check(double x)
{
double lt = 0, rt = 0;
for(int i = 0; i < n; i++)
{
if(p[i] < x)
lt = max(lt, (x - p[i]) / v[i]);
else if(p[i] > x)
rt = max(rt, (p[i] - x) / v[i]);
}
tim = max(lt, rt);
return (lt >= rt);
} int main()
{
while(cin >> n)
{
double ma = 0;
for(int i = 0; i < n; i++) scanf("%lf", p + i), ma = max(ma, p[i]);
for(int i = 0; i < n; i++) scanf("%lf", v + i); double l = 0, r = ma;
while(r - l >= eps)
{
double mid = (r + l) / 2.0000;
if(check(mid))
r = mid;
else
l = mid;
//cout << mid << endl;
}
printf("%.10lf\n", tim);
}
return 0;
}