Codeforces Round #706 (Div. 2)-C. Diamond Miner-题解

110 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

@TOC

Codeforces Round #706 (Div. 2)-C. Diamond Miner

传送门 Time Limit: 1 second Memory Limit: 256 megabytes

Problem Description

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 (ac)2+(bd)2\sqrt{(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?

Input

The input consists of multiple test cases. The first line contains a single integer tt (1t101\le t\le 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 (1n1051 \le n \le 10^5) — the number of miners and mines.

Each of the next 2n2n lines contains two space-separated integers xx (108x108-10^8 \le x \le 10^8) and yy (108y108-10^8 \le y \le 10^8), 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 10510^5.

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 10910^{-9}.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is accepted if and only if abmax(1,b)109\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}.

Sample Input

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

Sample Onput

3.650281539872885
18.061819283610362
32.052255376143336

题目大意

横轴上有n个矿,纵轴上有n个矿工。 每个矿工挖一块矿,耗费能量是他与矿的距离。 问每个矿工一块矿最少总耗能是多少。


题目分析

x轴上,+x和-x到y轴上的一点的距离是相等的。故可用|x|来代替x。 y轴上,+y和-y到x轴上的一点的距离是相等的。故可用|y|来代替y。 这样就便于排序


解题思路

不难发现,这道题的关键点就在离原点最近的矿工先挖离原点最近的矿。 这样才能保证每个矿工挖矿的线不交叉(坐标轴上除外),这样正好总距离最短。 (不用看)


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
double x[100010];
double y[100010];
int main()
{
    int N;
    cin >> N;
    while (N--) //N组测试样例
    {
        int n;
        cin >> n;
        int sx = 0, sy = 0;
        for (int i = 0; i < n * 2; i++)
        {
            int tx = 0, ty = 0;
            scanf("%d%d", &tx, &ty); //读入
            if (tx == 0)
                y[sy++] = abs(ty); //用绝对值代替原来的数
            else
                x[sx++] = abs(tx); //为了便于排序
        }
        sort(x, x + n); //排
        sort(y, y + n); //序
        double s = 0;
        for (int i = 0; i < n; i++)
        {
            s += sqrt(x[i] * x[i] + y[i] * y[i]); //累加
        }
        printf("%.10lf\n", s); //输出
    }
    return 0;
}

同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:blog.csdn.net/Tisfy/artic…