Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.3 kB
5
Indexable
Never
#include<bits/stdc++.h>
#define int long long
#define double long double
#define endl '\n'
#define PB push_back
using namespace std;
const int N = 2e5 + 5;
const double EPS = 1e-13;
struct Pt{
    double x, y;
    Pt(double _x=0, double _y=0) : x(_x), y(_y){ }
    Pt operator+(Pt a){ return Pt(x+a.x, y+a.y); }
    Pt operator-(Pt a){ return Pt(x-a.x, y-a.y); }
    double operator*(Pt a){ return x*a.x + y*a.y; }
    double operator^(Pt a){ return x*a.y - y*a.x; }
};
Pt arr[N];
Pt brr[N];
double dist(Pt a, Pt b){
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
double qry(Pt a, Pt b, Pt c){
    Pt v1 = c - a; Pt v2 = b - a;
    Pt v3 = c - b; Pt v4 = a - b;
    if(v1*v2 < 0 || v3*v4 < 0)
        return min(dist(a, c), dist(b, c));
    double len = dist(a, b);
    double ret = abs(v1^v2)/len;
    return ret;
}
void solve(){
    int n, m;
    cin >> n;
    for(int i=0; i<n; i++){
        double a, b;
        cin >> a >> b;
        arr[i] = Pt(a, b);
    }
    cin >> m;
    for(int i=0; i<m; i++){
        double a, b;
        cin >> a >> b;
        brr[i] = Pt(a, b);
    }
    int nowa = 0, nowb = 0;
    double ans = dist(arr[0], brr[0]);
    double d1 = dist(arr[0], arr[1]), d2 = dist(brr[0], brr[1]);
    Pt a = arr[0], b = brr[0];
    while(1){
        if(nowa == n-1 || nowb == m-1) break;
        Pt xx = a, yy = b;
        if(abs(d1*(nowa+1) - d2*(nowb+1)) < EPS){
            nowa++; nowb++;
            if(nowa < n) a = arr[nowa];
            if(nowb < m) b = brr[nowb];
        }
        else if(d1*(nowa+1) < d2*(nowb+1)){
            nowa++;
            if(nowa < n) a = arr[nowa];
            if(brr[nowb+1].x == b.x) b.y += (brr[nowb+1].y>b.y) ? d1 : -d1;
            else b.x += (brr[nowb+1].x>b.x) ? d1 : -d1;
        }
        else{
            nowb++;
            if(nowb < m) b = brr[nowb];
            if(arr[nowa+1].x == a.x) a.y += (arr[nowa+1].y>a.y) ? d2 : -d2;
            else a.x += (arr[nowa+1].x>a.x) ? d2 : -d2;
        }

        Pt da = a - xx; Pt db = b - yy;
        ans = min(ans, qry(xx, xx+da-db, yy));
    }
    cout << fixed << setprecision(15) << ans << endl;
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int T = 1;
    while(T--){
        solve();
    }
}