本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@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 miners instead of in this game.
The mining area can be described as a plane. The miners can be regarded as points on the y-axis. There are diamond mines in the mining area. We can regard them as points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point ).
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 uses his hook to mine a diamond mine at the point , he will spend 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 () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer () — the number of miners and mines.
Each of the next lines contains two space-separated integers () and (), which represent the point to describe a miner's or a diamond mine's position. Either , meaning there is a miner at the point , or , meaning there is a diamond mine at the point . 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 and the number of points on the y-axis is equal to .
It's guaranteed that the sum of for all test cases does not exceed .
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 .
Formally, let your answer be , and the jury's answer be . Your answer is accepted if and only if .
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…