B - Meeting on the Line CodeForces - 1730B (2)

文章描述了一种数学问题,有n个人分布在坐标线上,每个人有自己的位置xi和穿衣时间ti。目标是找到一个位置x0,使得所有人从各自的位置到达x0并完成穿衣所需的时间总和最小。文章给出了一种二分查找的解决方案来找到这个最优位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第二种解法,其实应该是一样的

n people live on the coordinate line, the �i-th one lives at the point ��xi​ (1≤�≤�1≤i≤n). They want to choose a position �0x0​ to meet. The �i-th person will spend ∣��−�0∣∣xi​−x0​∣ minutes to get to the meeting place. Also, the �i-th person needs ��ti​ minutes to get dressed, so in total he or she needs ��+∣��−�0∣ti​+∣xi​−x0​∣ minutes.

Here ∣�∣∣y∣ denotes the absolute value of �y.

These people ask you to find a position �0x0​ that minimizes the time in which all �n people can gather at the meeting place.

Input

The first line contains a single integer �t (1≤�≤1031≤t≤103) — the number of test cases. Then the test cases follow.

Each test case consists of three lines.

The first line contains a single integer �n (1≤�≤1051≤n≤105) — the number of people.

The second line contains �n integers �1,�2,…,��x1​,x2​,…,xn​ (0≤��≤1080≤xi​≤108) — the positions of the people.

The third line contains �n integers �1,�2,…,��t1​,t2​,…,tn​ (0≤��≤1080≤ti​≤108), where ��ti​ is the time �i-th person needs to get dressed.

It is guaranteed that the sum of �n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print a single real number — the optimum position �0x0​. It can be shown that the optimal position �0x0​ is unique.

Your answer will be considered correct if its absolute or relative error does not exceed 10−610−6. Formally, let your answer be �a, the jury's answer be �b. Your answer will be considered correct if ∣�−�∣���(1,∣�∣)≤10−6max(1,∣b∣)∣a−b∣​≤10−6.

#include<bits/stdc++.h>
#define Acode ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
#define endl '\n'

const double eps = 1e-7;
const int N = 1e6;
int x[N], t[N]; double ans = 0;
int n;
bool check(double mid)
{
	double l = -1e9, r = 1e9;
	for (int i = 1; i <= n; i++)
	{
		if (mid < t[i]) return false;
		l = max(l, x[i] - mid + t[i]);
		r = min(r, x[i] + mid - t[i]);
	}
	if (l <= r)
	{
		ans = (l + r) / 2;
		return true;
	}
	else
		return false;
}

int main()
{
	Acode;
	int t1;
	cin >> t1;
	while (t1--)
	{
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> x[i];
		}
		for (int i = 1; i <= n; i++)
		{
			cin >> t[i];
		}
		double l = 0, r = 1e9;
		int cnt = 100;
		while (cnt--)
		{
			double mid = (l + r) / 2;
			if (check(mid)) l = mid;
			else r = mid;
		}
		printf("%.7f\n", ans);

	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值