Loading

Codeforces Round #706 (Div. 2) C. Diamond Miner(数学/几何)

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

The mining area can be described as a plane. The 𝑛n miners can be regarded as 𝑛n points on the y-axis. There are 𝑛n diamond mines in the mining area. We can regard them as 𝑛n 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) uses his hook to mine a diamond mine at the point (𝑐,𝑑)(c,d), he will spend (𝑎−𝑐)2+(𝑏−𝑑)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?

Input

The input consists of multiple test cases. The first line contains a single integer 𝑡t (1≤𝑡≤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 𝑛n (1≤𝑛≤1051≤n≤105) — the number of miners and mines.

Each of the next 2𝑛2n lines contains two space-separated integers 𝑥x (−108≤𝑥≤108−108≤x≤108) and 𝑦y (−108≤𝑦≤108−108≤y≤108), which represent the point (𝑥,𝑦)(x,y) to describe a miner's or a diamond mine's position. Either 𝑥=0x=0, meaning there is a miner at the point (0,𝑦)(0,y), or 𝑦=0y=0, meaning there is a diamond mine at the point (𝑥,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 𝑛n and the number of points on the y-axis is equal to 𝑛n.

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

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 𝑎a, and the jury's answer be 𝑏b. Your answer is accepted if and only if |𝑎−𝑏|max(1,|𝑏|)≤10−9|a−b|max(1,|b|)≤10−9.

Example

input

Copy

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

output

Copy

3.650281539872885
18.061819283610362
32.052255376143336

数学题...并不会证明...

首先发现x或者y的正负性其实没有影响,直接全化为正数,然后分别排序,对应相加即可。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
using namespace std;
int n;
int main() {
	freopen("data.txt", "r", stdin);
	int t;
	cin >> t;
	while(t--) {
		vector<int> m, d;
		cin >> n;
		for(int i = 1; i <= 2 * n; i++) {
			int x, y;
			cin >> x >> y;
			if(x == 0) {
				if(y < 0) y = -y;
				m.push_back(y);
			} else if(y == 0) {
				if(x < 0) x = -x;
				d.push_back(x);
			}
		}
		sort(m.begin(), m.end());
		sort(d.begin(), d.end());
		double ans = 0;
		for(int i = 0; i < m.size(); i++) {
			ans += 1.0 * sqrt(1.0 * m[i] * m[i] + 1.0 * d[i] * d[i]);
		}
 		printf("%.14lf\n", ans);
	}	
	return 0;
}
posted @ 2021-03-10 23:14  脂环  阅读(184)  评论(0编辑  收藏  举报