题解:2024牛客多校第四场 G
G Horse Drinks Water
题目描述
This problem is a variant of the General’s Horse Drinking Water problem.
Given that the general’s horse is at point \((x_G, y_G)\) , the tent is at point \((x_T, y_T)\) , and the river is located along the positive half of the x-axis and the positive half of the y-axis, find the shortest distance the horse needs to travel to drink water from the river and return to the tent.
这个问题是将军的马喝水问题的变种。
已知将军的马在点 \((x_G,y_G)\),帐篷在点 \((x_T,y_T)\),河流位于 x 轴的正半轴和 y 轴的正半轴上,求马从河边喝水返回帐篷所需的最短距离。
输入描述:
Each test contains multiple test cases. The first line contains the number of test cases \(t ( 1 \leq t \leq 10^5)\) . The description of the test cases follows.
Each test case consists of a single line containing four integers \(x_G, y_G, x_T, y_T(0 \leq x_G, y_G, x_T, y_T \leq 10^9)\) ,, describing the positions of the general’s horse and the tent.
每个测试包含多个测试用例。第一行包含测试用例的数量 \(t ( 1 \leq t \leq 10^5)\) 。测试用例的描述如下。
每个测试用例由一行组成,包含四个整数 \(x_G, y_G, x_T, y_T(0 \leq x_G, y_G, x_T, y_T \leq 10^9)\) ,描述了将军的马和帐篷的位置。
输出描述:
For each test case, output a decimal number representing the shortest distance the horse needs to travel to drink water from the river and return to the tent.
Your answer is considered correct if its absolute or relative error does not exceed \(10^{-9}\).
Formally, let your answer be \(a\), and the jury’s answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max(1, |b|)} \leq 10^{-9}\) .
针对每个测试案例,输出一个十进制数,代表马匹从河边喝水到返回帐篷所需的最短距离。
如果答案的绝对误差或相对误差不超过 \(10^{-9}\),则认为答案正确。
形式上,让您的答案为 \(a\),陪审团的答案为 \(b\)。当且仅当 \(\frac{|a - b|}{\max(1, |b|)} \leq 10^{-9}\) 时,你的答案才会被接受。
示例1
输入
5
1 3 3 3
1 1 1 1
1 5 4 2
11 4 5 14
19 1 9 810
输出
4
2
5.8309518948
18.8679622641
809.4844038028
备注:
题意
在x轴的正半轴和y轴的负半轴上有一条河流
有一匹马和一个帐篷,都在第一象限
给你这两个物体的坐标,求出:
马在去河流喝完水后回到帐篷的最短路径长度
题解
这是基本(高中?初中?)的数学题吧
由于两点之间线段最短
马只有可能从这两种路径去喝水
输出两者的最小值就就可以了
我的代码
#include <bits/stdc++.h>
#define int long long
int t;
int x,y,a,b;
void solve() {
std::cin >> x >> y >> a >> b;
double ans = sqrt((double)((x - a)*(x - a) + (y + b)*(y + b)));
printf("%.10f\n", std::min(sqrt((double)((x + a)*(x + a) + (y - b)*(y - b))),ans));
}
signed main(){
std::cin >> t;
while(t--) solve();
return 0;
}
(先水一篇)
posted on 2024-07-26 21:11 Jiejiejiang 阅读(8) 评论(0) 编辑 收藏 举报