首页 > 其他 > 详细

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

时间:2021-03-10 23:54:55      阅读:44      评论:0      收藏:0      [点我收藏+]

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;
}

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

原文:https://www.cnblogs.com/lipoicyclic/p/14514976.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!