题解:2024牛客暑期多校第4场——G [Horse Drinks Water]

Horse Drinks Water

https://ac.nowcoder.com/acm/contest/81599/G

英文题干

This problem is a variant of the General’s Horse Drinking Water problem.

Given that the general’s horse is at point , the tent is at point , 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.

Input:

Each test contains multiple test cases. The first line contains the number of test cases . The description of the test cases follows.

Each test case consists of a single line containing four integers , describing the positions of the general’s horse and the tent.

Output:

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 .

Formally, let your answer be , and the jury’s answer be . Your answer is accepted if and only if .

中文题干

这个问题是将军饮马问题的一个变种。

假设将军的马位于点,帐篷位于点,河流沿着x轴正半轴和y轴正半轴,找出马需要行进的最短距离,喝水后返回帐篷。

思路

简单的初中几何签到题

  1. 因为有横竖两个岸,我们只需要求马分别到两个岸喝水所需的最短长度(对称+勾股),然后求较小值即可(注意开long long以及小数位数的保留)。

AC代码

时间复杂度:O()

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int infmin = 0xc0c0c0c0;
const int infmax = 0x3f3f3f3f;
const int maxn = 5e5 + 10;



signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        int x1, x2, y1, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        
        int A1 = abs(x2 - x1);
        int B1 = abs(y2 + y1);
        int TG1 = A1 * A1 + B1 * B1;
        double X = sqrt((double)TG1);

        int A2 = abs(x2 + x1);
        int B2 = abs(y2 - y1);
        int TG2 = A2 * A2 + B2 * B2;
        double Y = sqrt((double)TG2);

        double Z = min(X, Y);
        
        int test = Z;
        if(test*test==min(TG1,TG2)){
            cout << Z << "\n";
            continue;
        }

        cout.precision(14);
        cout << fixed << Z << "\n";
    }

    return 0;
}
全部评论

相关推荐

06-13 12:13
已编辑
东北大学 射频工程师
26毕业的,日常实习还能找到吗
求实习的青提很想去大厂:目前应该还有hc吧,腾讯感觉还有hc,最近捞了我好几次,因为目前有offer,所以不准备面了,可以再找找,不行的话就找找中小厂试试,因为我之前也找了好久,准备放弃了,结果有个岗位流程特别顺利,然后就oc,只能说坚持下试试,万一呢💪
点赞 评论 收藏
分享
05-30 18:54
武汉商学院 Java
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务