欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1895B.Points and Minimum Distance
Points and Minimum Distance
You are given a sequence of integers of length . You have to split these integers into pairs; each pair will represent the coordinates of a point on a plane. Each number from the sequence should become the or coordinate of exactly one point. Note that some points can be equal.
After the points are formed, you have to choose a path that starts from one of these points, ends at one of these points, and visits all points at least once.
The length of path is the sum of distances between all adjacent points on the path. In this problem, the distance between two points and is defined as .
Your task is to form points and choose a path in such a way that the length of path is minimized.
Input
The first line contains a single integer () — the number of testcases.
The first line of each testcase contains a single integer () — the number of points to be formed.
The next line contains integers () — the description of the sequence .
Output
For each testcase, print the minimum possible length of path in the first line.
In the -th of the following lines, print two integers and — the coordinates of the point that needs to be visited at the -th position.
If there are multiple answers, print any of them.
Note
In the first testcase, for instance, you can form points and and start the path from the first point and end it at the second point. Then the length of the path will be .
In the second testcase, you can form points , , and , 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$.
Samples
2
2
15 1 10 5
3
10 30 20 20 30 10
9
10 1
15 5
20
20 20
10 30
10 30
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |