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;
}

 

posted @ 2020-04-04 21:40  SunCY  阅读(111)  评论(0编辑  收藏  举报