2020 ICPC Universidad Nacional de Colombia Programming Contest---A
Posted H-w-H
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2020 ICPC Universidad Nacional de Colombia Programming Contest---A相关的知识,希望对你有一定的参考价值。
A. Approach
题意:
给出 4 4 4个点, A , B , C , D A,B,C,D A,B,C,D,两个人 a , b a,b a,b,a从 A A A到 B B B,b从 C C C到 D D D,他们两个速度相同,问他们相距最近的距离大小(注:当某一个人到达终点时,他会停留在终点,而另一个人仍然会继续前进)
思路:
猜测:距离的他们两点的距离是一个关于时间的凹函数,然后就是三分去查找。
注意细节:当一个人已经到达终点时,可以肯定当前距离函数仍然是一个凹函数或者一个单调递增的函数,所以我们要再用一次三分,来找到当前段的最小值,再和上一段进行比较。
!!!:特判a的起点和终点在同一个点,b的起点和终点在同一个点。
(精度很难确定)
Code
#include<bits/stdc++.h>
using namespace std;
double len1, len2;
const double eps = 5e-7;
double ax, ay, bx, by, cx, cy, dx, dy;
double dis(double a, double b, double c, double d) {
return sqrt((a-c)*(a-c)+(b-d)*(b-d));
}
double f(double t) {
double u = ax + t*(bx-ax)/len1, v = ay + t*(by-ay)/len1;
double g = cx + t*(dx-cx)/len2, h = cy + t*(dy-cy)/len2;
return dis(u, v, g, h);
}
double ff(double t) {
double u = bx, v = by;
double g = cx + t*(dx-cx)/len2, h = cy + t*(dy-cy)/len2;
return dis(u, v, g, h);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &ax, &ay, &bx, &by, &cx, &cy, &dx, &dy);
len1 = dis(ax, ay, bx, by);
len2 = dis(cx, cy, dx, dy);
// cout << len1 << ' ' << len2 << endl;
if(len1 == 0 && len2 == 0) {
printf("%.12lf", dis(ax, ay, cx, cy));
return 0;
}
double l = 0, r = min(len1, len2), lmid, rmid, ans = 1e18;
while(len1 && len2 && r-l > eps) {
lmid = l+(r-l)/3;
rmid = r-(r-l)/3;
if(f(lmid) <= f(rmid)) r = rmid;
else l = lmid;
}
if(len1 && len2) ans = f(l);
if(len1 > len2) {
swap(len1, len2);
swap(ax, cx);
swap(bx, dx);
swap(ay, cy);
swap(by, dy);
}
l = len1, r = len2;
while(r - l > eps) {
lmid = l + (r-l)/3;
rmid = r - (r-l)/3;
if(ff(lmid) <= ff(rmid)) r = rmid;
else l = lmid;
}
ans = min(ans, ff(l));
printf("%.12lf", ans);
return 0;
}
以上是关于2020 ICPC Universidad Nacional de Colombia Programming Contest---A的主要内容,如果未能解决你的问题,请参考以下文章
2020 ICPC Universidad Nacional de Colombia Programming Contest---A
2019 ICPC Universidad Nacional de Colombia Programming Contest
2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest K - Random Numbers (dfs序 线段树+数论)
2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules, Teams