zoukankan      html  css  js  c++  java
  • codeforces 782B The Meeting Place Cannot Be Changed (三分)

    The Meeting Place Cannot Be Changed

    Problem Description

    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

    Examples Output

    2.000000000000

    Examples Input

    4
    5 10 3 2
    2 3 2 4

    Examples 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.

    题目链接:http://codeforces.com/problemset/problem/782/B


    题意:一条线上有n个人,每个人一个坐标值xi,每个人有一个最大行走速度vi,问如果要让这n个人走到线上某一个点,最少需要多少时间。(百度的)

    思路:(自己的)

    知道这个后就明朗了,我们只需要利用3分法(为什么用3分法 o.o 学长说的)循环去缩小那个最小的时间范围。

    三分的代码很简洁明了,自己看楼。

    接着要解决的一个难题 就是 怎么判断 某t 时间内,所有点是否都能移动到某个点上:

    思路:

    = =。图是丑了点。 每个圈都代表每个人在 某t 时间内的可以的运动范围。

    如果交集不为空,则 在t时间内能够移动一点上。

    代码实现见函数  bool run(double time);

    ps: 最后输出的时候 用cout<<r<<endl; 或printf("%lf ",r); 会WA。

    因为 这样输出是默认 输出保留小数点6位后输出 即四舍五入 到小数点后6位再输出的。。。坑死我了。。。


    AC代码如下:time:139ms

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 const int MAXN=60000+100;
     7 const double eps=0.000001;
     8 struct type_1
     9 {
    10     double x,v;//x 为人的坐标 v为速度
    11 }people[MAXN];
    12 int n=0;
    13 bool cmp(type_1 a,type_1 b)
    14 {
    15     return a.x<b.x;
    16 }
    17 void time(double &maxtime)//求[0,maxtime]中的maxtime 
    18 {
    19     maxtime=(people[n-1].x-people[0].x)/people[0].v;
    20     double m;
    21     for(int i=1;i<n;i++)
    22     {
    23         m=max((people[i].x-people[0].x)/people[i].v,(people[n-1].x-people[i].x)/people[i].v);
    24         if(m>maxtime)
    25             maxtime=m;
    26     }
    27 }
    28 bool run(double time)//判断t时间内是否都能到某个点
    29 {
    30     double x1,x2,temp1,temp2;
    31     x1=people[0].x-people[0].v*time;
    32     x2=people[0].x+people[0].v*time;
    33     for(int i=1;i<n;i++)
    34     {
    35         temp1=people[i].x-people[i].v*time;
    36         temp2=people[i].x+people[i].v*time;
    37         if(temp1>x1)
    38             x1=temp1;
    39         if(temp2<x2)
    40             x2=temp2;
    41         if(x1>x2)
    42             return false;
    43     }
    44     return true;
    45 }
    46 
    47 int main()
    48 {
    49     cin>>n;
    50     for(int i=0;i<n;i++)
    51         scanf("%lf",&people[i].x);
    52     for(int i=0;i<n;i++)
    53         scanf("%lf",&people[i].v);
    54     sort(people,people+n,cmp);
    55     double l=0,r;//时间范围[0,maxtime]
    56     time(r);
    57     double o1,o2;//三分后的两个点
    58     bool bo1,bo2;
    59     while(l+eps<r)//缩小到精度以内 结束循环。
    60     {
    61         o1=(2*l+r)/3;//时间三分后的time1
    62         o2=(2*r+l)/3;//时间三分后的time2
    63 //printf("[%.10lf,%.10lf] [%.10lf,%.10lf]
    ",o1,o2,l,r);
    64         bo1=run(o1);//纪录o1时间内所有点能否到达一点的真假
    65         bo2=run(o2);
    66 //cout<<"-"<<bo1<<" "<<bo2<<"-"<<endl;
    67         if(bo2==false)
    68             l=o2;
    69         else
    70         {
    71             r=o2;
    72             if(bo1==true)
    73                 r=o1;
    74             else
    75                 l=o1;
    76         }
    77     }
    78     printf("%.10llf
    ",r);//注意 在这里WA了半个多小时,最后加了 (.10) 后终于AC。
    79     //cout<<r<<endl;
    80     return 0;
    81 }

    2017-03-09 01:16:21

  • 相关阅读:
    extern C的作用详解
    UIWindow in iOS
    iOS会议和组织
    Fantageek翻译系列之《使用Autolayout显示变化高度的UITableViewCell》
    KVO的概述的使用
    Struts2 基于XML校验(易百教程)
    Maven项目中添加JDBC驱动
    org.dom4j.DocumentException: null Nested exception: null解决方法
    struts2中的数据类型自动转换
    struts2的拦截器
  • 原文地址:https://www.cnblogs.com/Twobox/p/6523764.html
Copyright © 2011-2022 走看看