【CodeForces 782B】 The Meeting Place Cannot Be Changed 二分

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
Output
2.000000000000
Input
4
5 10 3 2
2 3 2 4
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.

題意:一條直線上n個人,每個人在a[i]的位置具備v[i]每秒的速度,問在哪個點相遇時間花費最少

思路:

可以通過二分答案的方法來寫。每次枚舉到的位置,所用的時間花費取決於最慢到的那個人。所以,要想時間儘量少一些,就要將位置再往最慢的那個人的方向移動(控制L和R)。按照這個規則不斷二分最後結果即是最短時間

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-6;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll v[maxn];


int main()
{
    ll n;
    while(cin>>n)
    {
        double ans = 0;
        double L = inf , R = -1;
        rep(i,1,n) a[i] = read(), L = min(L,a[i]), R = max(a[i],R);
        rep(i,1,n) v[i] = read();
        double t;
        while(R-L>=eps)
        {
            double mid = (R+L) / 2.0;
            double late = 0; ll obj = 1;
            rep(i,1,n)
            {
                 t = (fabs((double)a[i]-mid)) / (double)v[i];
                if(late<t) obj = i , late = t , ans = t;
            }
            if(a[obj]>mid) L = mid;
            else R = mid;
        }
        double to = (L+R)/2.0;
        printf("%lf\n",ans);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章