HDU 4717 三分

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 3E2 + 10;
const double eps = 1E-8;
int n, T, kase;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {};
	void read()
	{
		scanf("%lf%lf", &x, &y);
	}
};
Point p[maxn], v[maxn];
double Distance(const Point a, const Point b)
{
	return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double cal(double t)
{
	double ans = 0;
	for (int i = 0; i < n; i++)
		for (int j = i + 1; j < n; j++)
			ans = max(ans, Distance(Point(p[i].x + t * v[i].x, p[i].y + t * v[i].y), Point(p[j].x + t * v[j].x, p[j].y + t * v[j].y)));
	return ans;
}
double solve(double l, double r)
{
	while (r - l >= eps)
	{
		double lm = (l * 2 + r) / 3;
		double rm = (l + r * 2) / 3;
		if (cal(rm) - cal(lm) > eps) r = rm;
		else l = lm;
	}
	return l;
}
int main(int argc, char const *argv[])
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			p[i].read(), v[i].read();
		double ans = solve(0, 1E8);
		printf("Case #%d: ", ++kase);
		if (n == 1) printf("0.00 0.00\n");
		else printf("%.2lf %.2lf\n", ans, cal(ans));
	}
	return 0;
}


最大值的最小问题,典型的做法是二分,此题由于距离是二次函数,所以三分。

n个点,给出初始坐标和移动的速度和移动的方向,求在哪一时刻任意两点之间的距离的最大值的最小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值