Codeforces Round #365 (Div. 2) C. Chris and Road(计算几何+二分)

本文介绍了一种用于判断行人是否能在车辆到达前安全过马路的算法。该算法考虑了两种情况:车辆尚未到达行人所在的一侧,或者车辆已经通过行人所在的一侧。通过直接判断和二分搜索来确定行人能否安全过街的时间。

【解题方法】比较显然的是有两种情况,一种是车还在x轴另一边的时候人就已经走过去了,一种是车完全过去了的时候人走到,那么第一种情况直接先判一下,第二种情况二分时间然后同样算一下就可以了。

【AC 代码】

#include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
const int maxn=10050;
int judge(double x){
    if(x>eps) return 1;
    else if(x<-eps) return -1;
    else return 0;
}
double u,v,w;int n;
struct node{
    double x,y;
}p[maxn];
bool check1(){
    for(int i=0; i<n; i++){
        if(judge(u*p[i].x-v*p[i].y)<0) return 0;
    }
    return 1;
}
bool check2(double t){
    for(int i=0; i<n; i++){
        if(judge(u*p[i].x-v*p[i].y-u*v*t)>0) return 0;
    }
    return 1;
}
int main()
{
    cin>>n>>w>>v>>u;
    for(int i=0; i<n; i++) cin>>p[i].x>>p[i].y;
    //cout<<w<<" "<<u<<endl;
    if(check1()){
        printf("%.10f\n",1.0*w/u);
    }else{
        double l=0,r=2e9;
        for(int i=0; i<100; i++){
            double mid=(l+r)/2;
            if(check2(mid)) r=mid;
            else l=mid;
        }
        //double ans=(l+r)/2.0;
        printf("%.10f\n",1.0*w/u+(l+r)/2);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值