A. Diamond Miner

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

Diamond Miner is a game that is similar to Gold Miner, but there are nn miners instead of 11 in this game.

The mining area can be described as a plane. The nn miners can be regarded as nn points on the y-axis. There are nn diamond mines in the mining area. We can regard them as nn points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point (0,0)(0,0)).

Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point (a,b)(a,b) uses his hook to mine a diamond mine at the point (c,d)(c,d), he will spend (a−c)2+(b−d)2−−−−−−−−−−−−−−−√(a−c)2+(b−d)2 energy to mine it (the distance between these points). The miners can't move or help each other.

The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?

2.Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤101≤t≤10) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of miners and mines.

Each of the next 2n2n lines contains two space-separated integers xx (−108≤x≤108−108≤x≤108) and yy (−108≤y≤108−108≤y≤108), which represent the point (x,y)(x,y) to describe a miner's or a diamond mine's position. Either x=0x=0, meaning there is a miner at the point (0,y)(0,y), or y=0y=0, meaning there is a diamond mine at the point (x,0)(x,0). There can be multiple miners or diamond mines at the same point.

It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to nn and the number of points on the y-axis is equal to nn.

It's guaranteed that the sum of nn for all test cases does not exceed 105105.

3.Output

For each test case, print a single real number — the minimal sum of energy that should be spent.

Your answer is considered correct if its absolute or relative error does not exceed 10−910−9.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−9|a−b|max(1,|b|)≤10−9.

4.Examples

4.1input

3
2
0 1
1 0
0 -1
-2 0
4
1 0
3 0
-5 0
6 0
0 3
0 1
0 2
0 4
5
3 0
0 4
0 -3
4 0
2 0
1 0
-3 0
0 -10
0 -2
0 -10

4.2output

3.650281539872885
18.061819283610362
32.052255376143336

5.Code

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

int readint() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

int main() {
    int T = readint();
    while (T--) {
        int n = readint();
        int n1 = 0, n2 = 0;
        int x, y;
        for (int i = 1; i <= n + n; i++) {
            x = readint();
            y = readint();
            if (!x)
                a[++n1] = abs(y);
            else
                b[++n2] = abs(x);
        }
        sort(a + 1, a + n1 + 1);
        sort(b + 1, b + n2 + 1);
        long double ans = 0;
        for (int i = 1; i <= n1; i++)
            ans += sqrt((long double)a[i] * a[i] + (long double)b[i] * b[i]);
        printf("%.10Lf\n", ans);
    }
    return 0;
}

6.Conclusion

        上面的代码实现了一个核心功能:计算给定坐标点对之间的欧几里德距离的和。
具体来说,代码的核心功能如下所示:

1.readint() 函数用于从输入中读取一个整数。它处理了负数和多位数的情况。
2.主函数开始时读取测试用例的数量 T。
3.在 while 循环中依次处理每个测试用例:

        读取表示坐标对数量的整数 n。

        初始化计数变量 n1 和 n2 为 0。这些变量将分别统计位于 y 轴上和非 y 轴上的坐标点对的个数。
        进入一个循环,循环次数为 n + n。每次迭代中,读取两个整数 x 和 y,表示一个坐标点对。
        如果 x 为零,即该坐标位于 y 轴上,将 abs(y) 存储在数组 a 的第 n1 + 1 个位置,并将 n1 增加。
        如果 x 非零,即该坐标不在 y 轴上,将 abs(x) 存储在数组 b 的第 n2 + 1 个位置,并将 n2 增加。
        处理完所有坐标后,对数组 a 和 b 分别进行升序排序。

4.初始化变量 ans 为 0,用于保存最终的计算结果。
5.进入一个循环,循环次数为 n1。每次迭代中,计算 a[i] 和 b[i] 的平方和的平方根,并将结果累加到 ans 中。
6.最后,使用 printf 函数将 ans 的值以保留十位小数的格式输出。

        总而言之,该代码的核心功能是计算给定坐标点对之间的欧几里德距离的和,并将结果打印出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向阳而生__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值