The Meeting Place Cannot Be Changed CodeForces

  先对人的坐标升序排个序,二分时间即可,check函数的话就是判断一下mid时间内能否到,先假设全部人往右走mid时间,求出往右走到达的位置的最小值tmp1,然后取最右的人的坐标和tmp1比较,1.若该坐标大于tmp1,说明该人及其右边的人往右边走是到不了tmp1的。2.若该坐标小于等于tmp1,说明了全部人都可以在mid时刻内到达tmp1位置,直接就return true。对于第一种情况还要继续判断坐标大于tmp1的人往左走能 不能到达tmp1.

#include<bits/stdc++.h>
using namespace std;
const int inf=2e9;
#define eps 1e-7
struct node
{
    int x,v;
}p[60000+10];
int n;
bool cmp(const node&a,const node&b)
{
    return a.x<b.x;
}
bool check(double mid)
{
    double tmp1=inf,tmp2=-inf;
    for(int i=1;i<=n;i++)
        tmp1=min(tmp1,p[i].x*1.0+p[i].v*mid);
    if(p[n].x<=tmp1)
        return true;
    int pos;
    for(int i=1;i<=n;i++)
        if(p[i].x>tmp1)
        {
            pos=i;
            break;
        }
    for(int i=pos;i<=n;i++)
        tmp2=max(tmp2,p[i].x*1.0-p[i].v*mid);
    if(tmp2<=tmp1)
        return true;
    else
        return false;


}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>p[i].x;
    for(int i=1;i<=n;i++)
        cin>>p[i].v;
    sort(p+1,p+n+1,cmp);
    double low=0,up=inf,mid,ans;
    while(fabs(low-up)>=eps)
    {
        mid=(low+up)/2;
        if(check(mid))
        {
            ans=mid;
            up=mid;
        }
        else
            low=mid;
    }
    printf("%.12lf
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/eason9906/p/11754897.html