CF1895B.Points and Minimum Distance

入门

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a sequence of integers aa of length 2n2n. You have to split these 2n2n integers into nn pairs; each pair will represent the coordinates of a point on a plane. Each number from the sequence aa should become the xx or yy coordinate of exactly one point. Note that some points can be equal.

After the points are formed, you have to choose a path ss that starts from one of these points, ends at one of these points, and visits all nn points at least once.

The length of path ss is the sum of distances between all adjacent points on the path. In this problem, the distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1-x_2| + |y_1-y_2|.

Your task is to form nn points and choose a path ss in such a way that the length of path ss is minimized.

给你一个长度为 2n2n 的整数序列 aa。你需要将这 2n2n 个整数划分为 nn 个数对;每个数对表示平面上的一个点的坐标。序列 aa 中的每个数必须恰好作为某一个点的 xx 坐标或 yy 坐标。注意,某些点可以是相同的。

在构造出这些点之后,你需要选择一条路径 ss,该路径从这些点中的某一个出发,终止于这些点中的某一个,并且至少经过全部 nn 个点一次。

路径 ss 的长度定义为路径上所有相邻点之间的距离之和。本题中,两点 (x1,y1)(x_1, y_1) 与 (x2,y2)(x_2, y_2) 之间的距离定义为 ∣x1−x2∣+∣y1−y2∣|x_1-x_2| + |y_1-y_2|。

你的任务是构造 nn 个点并选择路径 ss,使得路径 ss 的长度最小。

输入格式

The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of testcases.

The first line of each testcase contains a single integer nn (2≤n≤1002 \le n \le 100) — the number of points to be formed.

The next line contains 2n2n integers a1,a2,…,a2na_1, a_2, \dots, a_{2n} (0≤ai≤1 0000 \le a_i \le 1\,000) — the description of the sequence aa.

第一行包含一个整数 tt(1≤t≤1001 \le t \le 100)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤1002 \le n \le 100)—— 待构造的点的数量。

下一行包含 2n2n 个整数 a1,a2,…,a2na_1, a_2, \dots, a_{2n}(0≤ai≤1 0000 \le a_i \le 1\,000)—— 序列 aa 的描述。

输出格式

For each testcase, print the minimum possible length of path ss in the first line.

In the ii-th of the following nn lines, print two integers xix_i and yiy_i — the coordinates of the point that needs to be visited at the ii-th position.

If there are multiple answers, print any of them.

对于每个测试用例,在第一行输出路径 ss 的最小可能长度。

在接下来的 nn 行中,第 ii 行输出两个整数 xix_i 和 yiy_i —— 表示需要在第 ii 个位置访问的点的坐标。

如果存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    2
    2
    15 1 10 5
    3
    10 30 20 20 30 10

    输出#1

    9
    10 1
    15 5
    20
    20 20
    10 30
    10 30

说明/提示

In the first testcase, for instance, you can form points (10,1)(10, 1) and (15,5)(15, 5) and start the path ss from the first point and end it at the second point. Then the length of the path will be ∣10−15∣+∣1−5∣=5+4=9|10 - 15| + |1 - 5| = 5 + 4 = 9.

In the second testcase, you can form points (20,20)(20, 20), (10,30)(10, 30), and (10,30)(10, 30), and visit them in that exact order. Then the length of the path will be ∣20−10∣+∣20−30∣+∣10−10∣+∣30−30∣=10+10+0+0=20|20 - 10| + |20 - 30| + |10 - 10| + |30 - 30| = 10 + 10 + 0 + 0 = 20.

在第一个测试用例中,例如,你可以构造点 (10,1)(10, 1) 和 (15,5)(15, 5),并使路径 ss 从第一个点开始、在第二个点结束。此时路径的长度为 ∣10−15∣+∣1−5∣=5+4=9|10 - 15| + |1 - 5| = 5 + 4 = 9。

在第二个测试用例中,你可以构造点 (20,20)(20, 20)、(10,30)(10, 30) 和 (10,30)(10, 30),并按此确切顺序访问它们。此时路径的长度为 ∣20−10∣+∣20−30∣+∣10−10∣+∣30−30∣=10+10+0+0=20|20 - 10| + |20 - 30| + |10 - 10| + |30 - 30| = 10 + 10 + 0 + 0 = 20。

输入解题思路,AI测评打分。不知道怎么写?

首页