cf中的一道二分题

来源:互联网 发布:js全角数字转半角 编辑:程序博客网 时间:2024/05/29 14:49
B. The Meeting Place Cannot Be Changed
time limit per test5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
Note

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个人的坐标,以及他们各自的移动速度,在坐标系上选择一个集合点,求所有人移动到集合点的最小时间,并输出。要求结果与标准答案误差不超过10的-6次方,一道二分题目。昨天看了这道题目本来想挂机的,结果挣扎了一下还真的做出来了。这道题目是对时间进行二分操作。

#include <iostream>#include <stdio.h>#include <cstring>#include <algorithm>using namespace std;#define Max_N 60005int N;struct p {  int x;  int v;  bool operator < (const p r)const {    return x < r.x;  }}man[Max_N];int main(){  int minv = 1e9 + 5;  double  mint, maxt;  scanf("%d", &N);  for (int i = 0; i < N; i++)    scanf("%d", &man[i].x);  for (int i = 0; i < N; i++) {    scanf("%d", &man[i].v);    if (minv > man[i].v) minv = man[i].v;  }  sort(man,man + N);  // 将所有人的坐标按从小到大排序。  maxt = (man[N-1].x - man[0].x) * 1.0 / minv;  //用最大的x减去最小的x并除以最小速度,就是整个集合所需要的最大时间,然后对这个时间进行二分处理。  mint = 0;  double t = (maxt + mint )/2;  double l, r;  int flag;  while (true) {     flag = 0;    double mi = (maxt + mint )/2;    l = man[0].x - man[0].v * mi;    r = man[0].x + man[0].v * mi;    if(maxt - mint < 1e-7 ) break;   //当最大速度与最小速度之差小于10e-7就跳出。    for (int i = 1; i < N ; i++) {   //这个是对每个速度求人们能否汇聚到一个点,如果可以就让最大速度更新为这个值,否则就另最小速度更新为这个值。      double l1 = man[i].x - man[i].v * mi;      double r1 =  man[i].x + man[i].v * mi;     l = max(l, l1);     r = min(r, r1);     if(l > r) {       flag = 1;       break;     }    }    if (flag == 1) mint = mi;    else maxt = mi;  }  t = (maxt + mint )/2;  printf("%.12lf\n", t);  return 0;}




0 0
原创粉丝点击