CF706A Beru-taxi 题解

Content

有一个人在点 \((a,b)\) 等出租车。已知他周围共有 \(n\) 辆出租车,其中第 \(i\) 辆车在点 \((x_i,y_i)\) 上,速度为 \(v_i\)。这个人想打能让他最早上车的出租车,请问这辆出租车需要多久才能到达这个人的位置?

数据范围:\(-100\leqslant a,b,x_i,y_i\leqslant 100,1\leqslant n\leqslant 1000,1\leqslant v_i\leqslant 100\)

Solution

直接暴力计算每辆车到达这个人位置的时间。因为距离为 \(\sqrt{(x_i-a)^2+(y_i-b)^2}\),速度为 \(v_i\),所以第 \(i\) 辆车到达的时间为 \(\dfrac{\sqrt{(x_i-a)^2+(y_i-b)^2}}{v_i}\)。然后按照时间为关键字从小到大排序,输出第一个即可。

Code

#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

int n;
double ax, ay;
struct node {
	double x, y, v, dist;
	bool operator < (const node& ss) const {return dist < ss.dist;}
}a[100007];

int main() {
	scanf("%lf%lf%d", &ax, &ay, &n);
	for(int i = 1; i <= n; ++i)	{
		scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].v);
		a[i].dist = sqrt((ax - a[i].x) * (ax - a[i].x) + (ay - a[i].y) * (ay - a[i].y)) / a[i].v;
	}
	sort(a + 1, a + n + 1);
	printf("%.20lf", a[1].dist);
}
posted @ 2021-12-21 20:17  Eason_AC  阅读(29)  评论(0)    收藏  举报