AtCoder Beginner Contest 426

E - Closest Moment

Takahashi and Aoki walk on a two-dimensional plane.
Takahashi's start point is (TS_X, TS_Y) and goal point is (TG_X, TG_Y). Aoki's start point is (AS_X, AS_Y) and goal point is (AG_X, AG_Y).
They simultaneously depart from their respective start points and walk straight toward their respective goal points at speed 1, and stop when they reach their respective goal points. (Note that they depart simultaneously, but they do not necessarily stop at the same time.)
Find the distance between them at the moment when the distance between them is shortest (including the moment they depart and after they stop).
Here, distance refers to Euclidean distance. That is, the distance between two points (x_1,y_1),(x_2,y_2) is defined as \sqrt{(x_1-x_2)^2+(y_1-y_2)^2}.
You are given T test cases, so solve each of them.

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const double eps=1e-7;
double dist(double x,double y){
    return sqrt(x*x+y*y);
}
pair<double,double> dv1,dv2;
int x_1,y_1,x_2,y_2;
int x_3,y_3,x_4,y_4;
double calc_both(double t){
    double x=x_1+dv1.fi*t;
    double y=y_1+dv1.se*t;
    double u=x_3+dv2.fi*t;
    double v=y_3+dv2.se*t;
    return dist(x-u,y-v);
}
double calc_alone(double t){
    double u=x_3+dv2.fi*t;
    double v=y_3+dv2.se*t;
    return dist(x_2-u,y_2-v);
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>x_1>>y_1>>x_2>>y_2;
        cin>>x_3>>y_3>>x_4>>y_4;

        int a=x_2-x_1,b=y_2-y_1;
        double t1=dist(a,b);
        dv1.fi=a/t1;
        dv1.se=b/t1;

        a=x_4-x_3,b=y_4-y_3;
        double t2=dist(a,b);
        dv2.fi=a/t2;
        dv2.se=b/t2;
        if(t1>t2){
            swap(t1,t2);
            swap(dv1,dv2);
            swap(x_1,x_3),swap(y_1,y_3);
            swap(x_2,x_4),swap(y_2,y_4);
        }
        double l=0, r=t1;
        while(r-l>eps){
            double lmid = l + (r - l) / 3;
            double rmid = r - (r - l) / 3;
            if(calc_both(rmid) >= calc_both(lmid))  r = rmid;
            else    l = lmid;
        }
        double ans=calc_both(l);
        l=t1, r=t2;
        while(r-l>eps){
            double lmid = l + (r - l) / 3;
            double rmid = r - (r - l) / 3;
            if(calc_alone(rmid) >= calc_alone(lmid))    r = rmid;
            else    l = lmid;
        }
        ans=min(ans,calc_alone(l));
        cout<<fixed<<setprecision(7)<<ans<<'\n';
    }
    return 0;
}

AtCoder Inc.'s online shop currently handles N products, and the stock of product i is A_i units remaining.
Process the following Q orders in order. The i-th order is as follows:

  • Buy k_i units each of products l_i,l_i+1,\dots,r_i. For products with fewer than k_i units, buy all available units. Report the total number of products bought in this order.
    Note that for i&lt;Q, the stock of products bought in the i-th order is reduced before proceeding to the (i+1)-th order.

参考题解

三分模板 - lifehappy - 博客园

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容