The Meeting Place Cannot Be Changed CodeForces - 780B 二分+更新值

这个题不难,就是想记录一下这个更新的方式,很漂亮!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include<iomanip>
#include <queue>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const int N = 6e4+5;
const double EPS = 1e-7;
typedef long long ll;
double x[N],v[N];
int n;
bool Update( double& a,double& b,double c,double d ){
    if( c>b || d<a ) return 1;
    a = max(a,c);
    b = min(b,d);
    return 0;
}
bool check(double s){
    int flag = 1;
    double L = x[1]-v[1]*s,R = x[1]+v[1]*s;
    if( L>R ) swap(L,R);
    _for(i,1,n+1){
        double l = x[i] - v[i]*s,r = x[i] + v[i]*s; 
        if( l>r ) swap(l,r);
        if( Update( L,R,l,r ) ){
            flag = 0;
            break;
        } 
    }
    return flag;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    _for(i,1,n+1) cin>>x[i];
    _for(i,1,n+1) cin>>v[i];
    double L = 0,R = 1e9+5;
    while(fabs(R-L) > EPS ) {
        double mid= (R+L)/2;
        if( check(mid) ) R = mid;
        else L = mid;
    } 
    cout<<fixed<<setprecision(9)<<(R+L)/2<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛阳八中我最棒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值